1

我正在开发 iOS 6 中的应用程序,我是初学者。我在这里通过几个线程进行了大量研究,我终于到了需要问我问题的地步。

我正在尝试实现一个包含两个相同大小的子视图的单视图应用程序,在纵向模式下,ViewA 在顶部,ViewB 在底部。两个视图的大小应该相同,并且不应该有填充。

我觉得我正处于弄清楚这一点的边缘。但是,我只是不了解程序约束。我的顶视图是红色(视图 A),我的底视图(视图 B)是绿色。

当我运行以下代码时,整个屏幕都是绿色的。我在某处遗漏了一些东西:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.topView.translatesAutoresizingMaskIntoConstraints = NO;
    self.bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view removeConstraints:[self.view constraints]];

    NSDictionary *views = @{@"topView":self.topView, @"bottomView":self.bottomView};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView]-0-[bottomView]|" options:0 metrics:nil views:views]];
}

我已经使用大量讨厌的 Frame/Bounds 代码手动完成了这个示例。因此,我正在尝试使用程序化自动布局功能来完成这项工作。

我的问题实际上还有第二部分。我这样做的主要原因是当我旋转到横向时,我需要稍微改变视图的顺序。回顾一下,纵向是视图 B 之上的视图 A。在横向视图中,我需要视图 A 位于视图 B 的左侧。同样,我已经使用讨厌的帧/边界代码完成了这个转换。

这个想法是1)删除约束,2)在检测到的旋转变化上交换新的约束。关于这种方法的任何想法或指示?为了走这条路,我首先需要弄清楚我的问题的第一部分出了什么问题。

我非常感谢提供的任何帮助。

更新: 我想我已经弄清楚了问题所在。我没有告诉顶部和底部视图它们的大小“|[topView(bottomView)][bottomView(topView)]|” 这似乎现在起作用了。我还编写了景观转换代码。我仍然不是 100% 认为这是正确的做法,我希望得到一些建议。

这是代码:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.topView.translatesAutoresizingMaskIntoConstraints = NO;
    self.bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self updateConstraints];
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self updateConstraints];
}

-(void)updateConstraints
{
    [self.view removeConstraints:[self.view constraints]];

    NSDictionary *views = @{@"topView":self.topView, @"bottomView":self.bottomView};

    if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomView]|" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(bottomView)]-0-[bottomView(topView)]|" options:0 metrics:nil views:views]];
    }
    else
    {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView]|" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[bottomView]|" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView(bottomView)]-0-[bottomView(topView)]|" options:0 metrics:nil views:views]];
    }
}

@end
4

0 回答 0