Abdalrahman Shatou
Abdalrahman Shatou

Reputation: 4758

Disable Autolayout Localization Behavior (RTL - Right To Left Behavior )

My application is localized in both English and Arabic.

Unfortunately, sometimes the autolayout behavior for localization is not required. By that, I mean reversing the leading and trailing spaces. I want to override this behavior. Is there any way to do that?

Upvotes: 33

Views: 9881

Answers (5)

Somebody
Somebody

Reputation: 733

another easy way

Select any view from the storyboard

from the attributes inspector on the right select set "Semantic" to "Force Left-to-Right"

Upvotes: 2

Mohammad Zekrallah
Mohammad Zekrallah

Reputation: 449

as in @Pavel answer, you should turn off 'Respect language direction' property. if you have lots of constraints, you can open xib or storyboard file in XML view and replace all 'leading' values with 'left' and all 'trailing' values with 'right' and you're done.

Upvotes: 4

hbk
hbk

Reputation: 11184

Try this

  1. Create class for managing constraint in all views
@implementation RTLController

#pragma mark - Public

- (void)disableRTLForView:(UIView *)view
{
    [self updateSubviewForParentViewIfPossible:view];
}

#pragma mark - Private

- (void)updateConstraintForView:(UIView *)view
{
    NSMutableArray *constraintsToRemove = [[NSMutableArray alloc] init];
    NSMutableArray *constraintsToAdd = [[NSMutableArray alloc] init];

    for (NSLayoutConstraint *constraint in view.constraints) {

        NSLayoutAttribute firstAttribute = constraint.firstAttribute;
        NSLayoutAttribute secondAttribute = constraint.secondAttribute;

        if (constraint.firstAttribute == NSLayoutAttributeLeading) {
            firstAttribute = NSLayoutAttributeLeft;
        } else if (constraint.firstAttribute == NSLayoutAttributeTrailing) {
            firstAttribute = NSLayoutAttributeRight;
        }

        if (constraint.secondAttribute == NSLayoutAttributeLeading) {
            secondAttribute = NSLayoutAttributeLeft;
        } else if (constraint.secondAttribute == NSLayoutAttributeTrailing) {
            secondAttribute = NSLayoutAttributeRight;
        }

        NSLayoutConstraint *updatedConstraint = [self constraintWithFirstAttribute:firstAttribute secondAtribute:secondAttribute fromConstraint:constraint];
        [constraintsToRemove addObject:constraint];
        [constraintsToAdd addObject:updatedConstraint];

    }

    for (NSLayoutConstraint *constraint in constraintsToRemove) {
        [view removeConstraint:constraint];
    }
    for (NSLayoutConstraint *constraint in constraintsToAdd) {
        [view addConstraint:constraint];
    }
}

- (void)updateSubviewForParentViewIfPossible:(UIView *)mainView
{
    NSArray *subViews = mainView.subviews;
    [self updateConstraintForView:mainView];

    if (subViews.count) {
        for (UIView * subView in subViews) {
            [self updateConstraintForView:subView];
            [self updateSubviewForParentViewIfPossible:subView];
        }
    }
}

- (NSLayoutConstraint *)constraintWithFirstAttribute:(NSLayoutAttribute)firstAttribute secondAtribute:(NSLayoutAttribute)secondAttribute fromConstraint:(NSLayoutConstraint *)originalConstraint
{
    NSLayoutConstraint *updatedConstraint =
    [NSLayoutConstraint constraintWithItem:originalConstraint.firstItem
                                 attribute:firstAttribute
                                 relatedBy:originalConstraint.relation
                                    toItem:originalConstraint.secondItem
                                 attribute:secondAttribute
                                multiplier:originalConstraint.multiplier
                                  constant:originalConstraint.constant];
    return updatedConstraint;
}
@end
  1. Add this code in to controller that should disable RTL behavior
RTLController *rtl = [[RTLController alloc] init];
[rtl disableRTLForView:self.view];

Upvotes: 2

Pavel
Pavel

Reputation: 930

To make leading act always as left (and trailing always like right), i.e. make it language independent, you can remove checkmark on "Respect language direction" on all constrains.

You can find this checkmark in constrain settings in the Attributes inspector under "First Item" button.

Respect language direction

Upvotes: 42

Wain
Wain

Reputation: 119031

The attributes leading and trailing are the same as left and right for left-to-right languages such as English, but in a right-to-left environment such as Hebrew or Arabic, leading and trailing are the same as right and left. When you create constraints, leading and trailing are the default values. You should usually use leading and trailing to make sure your interface is laid out appropriately in all languages, unless you’re making constraints that should remain the same regardless of language.

So, for your special cases, don't use leading and trailing, instead, explicitly use left and right when you create your constraints.

Upvotes: 24

Related Questions