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

前端 未结 4 864
予麋鹿
予麋鹿 2020-12-08 14:19

My application is localized in both English and Arabic.

Unfortunately, sometimes the autolayout behavior for localization is not required. By that, I mean reversing

相关标签:
4条回答
  • 2020-12-08 15:02

    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

    0 讨论(0)
  • 2020-12-08 15:02

    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.

    0 讨论(0)
  • 2020-12-08 15:06

    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];
    
    0 讨论(0)
  • 2020-12-08 15:10

    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.

    0 讨论(0)
提交回复
热议问题