UIView to Ignore Orientation or Stick-To-Bottom Co

2019-05-21 05:45发布

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?

2条回答
欢心
2楼-- · 2019-05-21 06:14

You can set the autoresizing mask like this:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin;

This will keep the view at the bottom.

查看更多
对你真心纯属浪费
3楼-- · 2019-05-21 06:22

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;
    }
}
查看更多
登录 后发表回答