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

纵然是瞬间 提交于 2019-11-28 06:19:55

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.

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.

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.

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!