UIView忽略方向或粘贴到底部容器 [英] UIView to Ignore Orientation or Stick-To-Bottom Container

查看:95
本文介绍了UIView忽略方向或粘贴到底部容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以纵向模式在屏幕底部显示UIView,因此,当旋转手机并水平方向将重新定位/调整所有子视图的大小时,一个UIView将保持原样,并且具有相同的大小和原始位置(即,如果位于纵向模式的底部,则位于水平方向的右端).

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).

有一个好的方法吗?

推荐答案

我可以想到几种方法.我在下面显示的一种方法仅依赖于使用约束.为此,这三个按钮不应位于自己的透明视图中,而应是要旋转的视图的子视图(在我的示例中为self.view).我不认为这3个按钮的原始约束很重要,因为我在旋转时将其删除了,但是我开始使用的约束具有带有centerX约束的中心按钮,到底部的固定距离,到左边和右边的标准水平距离.右按钮,所有三个按钮的基线都对齐.在viewDidLoad中,我遍历了self.view的所有约束,并确定了所有与这些按钮有关的约束,并将它们放入数组中,以便我可以将其删除并稍后再添加回去.

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;
    }
}

这篇关于UIView忽略方向或粘贴到底部容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆