UIView忽略方向或从底到底的容器

时间:2013-07-18 14:35:08

标签: iphone ios objective-c uiview orientation

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

有没有好方法呢?

2 个答案:

答案 0 :(得分:0)

您可以像这样设置自动调整大小:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin;

这会使视图保持在底部。

答案 1 :(得分:0)

我可以想到几种方法来做到这一点。我在下面展示的一种方式完全依赖于使用约束。为此,3个按钮不应该在他们自己的透明视图中,而只是要旋转的视图的子视图(在我的示例中是self.view)。我不认为对这3个按钮的原始约束很重要,因为我在旋转时将它们移除,但是我开始使用的约束中心按钮具有centerX约束,到底部的固定距离,到左边的标准水平距离以及右按钮,所有三个按钮的基线对齐。在viewDidLoad中,我循环遍历所有self.view的约束并识别所有与这些按钮有关的内容,并将它们放入一个数组中,以便我可以删除它们并稍后再添加它们。

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