UIView to Ignore Orientation or Stick-To-Bottom Container

混江龙づ霸主 提交于 2019-12-22 13:39:28

问题


I want to display UIView on the bottom of the screen in portrait mode, so when a phone is rotated and horizontal orientation would reposition/resize all subviews, that one UIView would remain where it was, with the same size and original position (i.e. on the right end of horizontal orientation if it was on the bottom of portrait mode).

Is there a good way to do it?


回答1:


You can set the autoresizing mask like this:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin;

This will keep the view at the bottom.




回答2:


I can think of several ways to do this. One way, that I show below relies solely on using constraints. For this to work the 3 buttons should not be in their own transparent view, but just be subviews of the view you want to rotate (that is self.view in my example). I don't think the original constraints to these 3 buttons matters, because I remove them on rotation, but the constraints I started with had the center button with a centerX constraint, a fixed distance to the bottom, standard horizontal distances to the left and right buttons, and all three buttons had their baselines aligned. In viewDidLoad I loop through all of self.view's constraints and identify all that have to do with these buttons, and put them into an array so I can remove them and add them back later.

@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *portraitConstraints;
@property (strong,nonatomic) NSMutableArray *landscapeConstraints;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.portraitConstraints = [NSMutableArray array];
    self.landscapeConstraints = [NSMutableArray array];
    for (NSLayoutConstraint *con in self.view.constraints) {
        if (con.firstItem == self.leftButton || con.secondItem == self.leftButton || con.firstItem == self.centerButton || con.secondItem == self.centerButton || con.firstItem == self.rightButton || con.secondItem == self.rightButton) {
            [self.portraitConstraints addObject:con];
        }
    }
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

    switch (interfaceOrientation) {
        case UIInterfaceOrientationLandscapeRight:{
            [self.view removeConstraints:self.portraitConstraints];
            [self.view removeConstraints:self.landscapeConstraints];
            [self.landscapeConstraints removeAllObjects];
            NSLayoutConstraint *centerYCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
            NSLayoutConstraint *rightCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeRight relatedBy:0 toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-8];
            NSArray *stackCons= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}];
            [self.landscapeConstraints addObject:centerYCon];
            [self.landscapeConstraints addObject:rightCon];
            [self.landscapeConstraints addObjectsFromArray:stackCons];
            [self.view addConstraints:self.landscapeConstraints];
            break;
        }
        case UIInterfaceOrientationLandscapeLeft:{
            [self.view removeConstraints:self.portraitConstraints];
            [self.view removeConstraints:self.landscapeConstraints];
            [self.landscapeConstraints removeAllObjects];
            NSLayoutConstraint *centerYCon2 = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
            NSLayoutConstraint *leftCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:8];
            NSArray *stackCons2= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}];
            [self.landscapeConstraints addObject:centerYCon2];
            [self.landscapeConstraints addObject:leftCon];
            [self.landscapeConstraints addObjectsFromArray:stackCons2];
            [self.view addConstraints:self.landscapeConstraints];
            break;
        }
        case UIInterfaceOrientationPortrait:{
            [self.view removeConstraints:self.landscapeConstraints];
            [self.view addConstraints:self.portraitConstraints];
            break;
        }
        default:
            break;
    }
}


来源:https://stackoverflow.com/questions/17726461/uiview-to-ignore-orientation-or-stick-to-bottom-container

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