Iphone UIView忽略方向或粘贴到底部容器

Iphone UIView忽略方向或粘贴到底部容器,iphone,ios,objective-c,uiview,orientation,Iphone,Ios,Objective C,Uiview,Orientation,我想在纵向模式下在屏幕底部显示UIView,这样当手机旋转且水平方向将重新定位/调整所有子视图时,一个UIView将保持原样,大小和原始位置相同(即,如果在纵向模式的底部,则位于水平方向的右端) 有什么好办法吗 您可以如下设置自动调整大小掩码: myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin; 这将使视图保持在底部。我可以想出几种方法来实现这一点。我在下面展示的一种方法完全依赖于使用约束。为此,3个按钮不应该位于它

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


有什么好办法吗

您可以如下设置自动调整大小掩码:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin;

这将使视图保持在底部。

我可以想出几种方法来实现这一点。我在下面展示的一种方法完全依赖于使用约束。为此,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;
    }
}

我不擅长自动布局,但看起来改变方向是唯一的方法。此外,这个问题必须有帮助:我认为唯一的方法是不允许此控制器旋转,然后对要旋转的屏幕部分应用变换。是否希望此视图的任何子视图也保持不变以不旋转?因此,在此视图中,所有对象都将位于其一侧?子视图可以旋转,但保持在它们所在的位置。我在照相机里看到过!应用程序中的快门按钮容器总是主按钮所在的位置,尽管方向不同。这可能会变得复杂,但这取决于此视图是什么。请提供此视图的详细信息。这将旋转视图。是-它将停留在水平模式的底部,但我希望它停留在水平模式的右侧mode@Vad我有点搞不懂你说的呆在右边是什么意思,你能澄清一下吗?简单地说,我想要一个容器贴在主按钮总是在的角落里。所以,如果您处于纵向模式,它位于屏幕底部,如果您处于水平模式,它位于屏幕的右侧或左侧。@Vad,不,它们不会。如果您能在问题中提到您需要支持iOS 5,那就太好了(尽管我不知道您为什么要这样做。iOS 7即将面世,我认为iOS 6和iOS 7将是您需要支持的全部)。