【问题标题】:Custom view for portrait and and orientation using storyboard using single view controller使用单视图控制器使用情节提要的纵向和方向自定义视图
【发布时间】:2015-02-04 18:40:59
【问题描述】:

我有一个应用程序,要求我根据纵向或横向方向设置不同的图像视图和标签布局。

例如,在纵向模式下,我在顶部和底部有 2 个 UIImageView,它们均匀地共享屏幕。在横向模式下,2 个 UIImageViews 并排。如何使用 Xcode 6 中的情节提要 来做到这一点?

我尝试过的事情:

  1. 在情节提要中使用大小类设置视图约束。一世 在w:Any h:Any 中设置我的 2 个图像视图的基值,然后移动 到w:Compact h:Regular 并为纵向视图设置约束。然后 我搬到了w:Any h:Compact,并排移动了两个图像视图 并为横向视图设置约束。然后我遇到冲突 w:Compact h:Compact 因为我申请的限制 以及横向和纵向。我想是因为我搬家了 在设置之前的横向图像视图 导致此问题的约束。
  2. 使用 2 个单独的视图控制器并具有纵向视图控制器 使用方向更改时切换到横向视图控制器 NSNotificationCenter 和 performSegueWithIdentifier 推送 我的纵向视图控制器顶部的横向视图控制器。 这个问题是有很多数据要传递(很多 图像视图和标签)。

是一种只使用一个视图控制器的方法,以及一种重新排列图像视图/标签并在方向改变时对其应用新约束的方法吗? -(void)viewDidLayoutSubviews{} 是我把代码重新排列图像视图的地方吗?有人可以指出我正确的方向吗?谢谢!

【问题讨论】:

    标签: ios objective-c orientation landscape portrait


    【解决方案1】:

    对于这种情况,我建议通过代码以编程方式进行布局。 这是示例: https://dl.dropboxusercontent.com/u/48223929/Test.zip

    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        [self layoutImageViewsForViewSize:self.view.frame.size];
    }
    
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        [self layoutImageViewsForViewSize:size];
    }
    
    - (void)layoutImageViewsForViewSize:(CGSize)size
    {
        // Portrait mode
        if (size.height > size.width) {
            self.imageView1.frame = CGRectMake(0, 0, size.width, size.height / 2);
            self.imageView2.frame = CGRectMake(0, size.height / 2, size.width, size.height / 2);
            // Landscape mode
        } else {
            self.imageView1.frame = CGRectMake(0, 0, size.width / 2, size.height);
            self.imageView2.frame = CGRectMake(size.width / 2, 0, size.width / 2, size.height);
        }
    }
    

    对于自动布局:

    - (void)layoutImageViewsForViewSize:(CGSize)size
    {
        // Portrait mode
        [self.view removeConstraints:self.view.constraints];
        if (size.height > size.width) {
            NSString *heightLayoutFormatString = [NSString stringWithFormat:@"V:|[_imageView1(%.0f)][_imageView2(%.0f)]|", size.height / 2, size.height / 2];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:heightLayoutFormatString options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1, _imageView2)]];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[_imageView1(%.0f)]|", size.width] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)]];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|[_imageView2(%.0f)]|", size.width] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView2)]];
    
        // Landscape mode
        } else {
            NSString *widthLayoutFormatString = [NSString stringWithFormat:@"H:|[_imageView1(%.0f)][_imageView2(%.0f)]|", size.width / 2, size.width / 2];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:widthLayoutFormatString options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1, _imageView2)]];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_imageView1(%.0f)]|", size.height] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView1)]];
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[_imageView2(%.0f)]|", size.height] options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView2)]];
        }
        [self.view setNeedsUpdateConstraints];
    }
    

    自动布局的另一种解决方案 - 制作包含 2 个图像视图的 UIView 的自定义子类,并在没有约束的情况下以编程方式对其进行布局 - 只需 setFrame。 然后将此自定义 UIView 添加到您的故事板

    【讨论】:

    • 上面的代码可以用storyboard代替xib吗?
    • 是的,但是会有问题:你需要禁用自动布局。并且它将在整个情节提要中被禁用。另一种解决方案 - 使用故事板和自动布局,但不直接更改框架,仅更改约束
    • 如果我使用故事板和自动布局,我是否必须以编程方式更改约束?我尝试在情节提要的视图控制器中移动图像视图(用于侧向和顶部和底部),并在之前对不同的大小类应用约束,最终导致很多冲突。是否可以以编程方式分别为横向和纵向应用一组约束?
    • 自动布局的更新答案
    • 我使用 self.imageView1 和 self.imageView2 访问我的图像视图并得到错误:*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析约束格式:self 不是视图字典中的键。五:|[self.imageView1(334)][self.imageView2(334)]|
    猜你喜欢
    • 1970-01-01
    • 2013-01-27
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多