iOS自动布局和纵向和横向的大小类

时间:2016-07-02 03:54:38

标签: ios

我使用自动布局和尺寸类设计我的布局。现在我的肖像画布局了 enter image description here

点击更改按钮后,蓝色视图的框架和红色视图的框架将进行交换

<com.mopub.mobileads.MoPubView
android:id="@+id/adview"
android:layout_width="fill_parent"
android:layout_height="50dp"
/>

现在看起来像:

enter image description here

然后我将iPhone的方向更改为横向,我希望红色视图将保持在顶部位置,但是,它会更改为: enter image description here

有人可以帮我弄清楚如何实现这个吗?谢谢你的帮助! 这是示例项目: https://github.com/SomnusLee1988/SamplePorject.git

1 个答案:

答案 0 :(得分:0)

我刚刚在你的github repo上提交了一个pull请求,它有我解决这个问题的方法。这是我所做的事情:

1)我从故事板中删除了两个视图,并在viewController中以编程方式添加了它们。

2)现在,约束以编程方式添加到viewController

中的两个视图

3)当点击Change按钮时,约束被移除,然后与之前的约束相反(即view1常量变为view2约束,view2约束变为view1约束)。

以下是一些相关代码:

@IBAction func change(sender: AnyObject) {
    // Remove existing constraints
    self.firstView.removeConstraints(self.firstView.constraints)
    self.secondView.removeConstraints(self.secondView.constraints)
    self.firstView.removeFromSuperview()
    self.secondView.removeFromSuperview()

    // Decide which view will recieve which constraints now based on their current sizes
    if firstView.frame.height == 50 {
        self.createAutoLayoutForViews(self.secondView, view2: self.firstView)
    } else {
        self.createAutoLayoutForViews(self.firstView, view2: self.secondView)
    }
}

要查看完整的实现,请参阅我的拉取请求或在github repo here

上分叉

注意:我只是将视图随机调整大小。如果您希望它们的大小不同,则需要更改两个视图的自动布局约束