iOS自动布局约束和框架无法正常工作

app*_*avs 5 constraints view parent-child ios autolayout

我有一个嵌套在View Controller中的Scroll View,它停放在Container中.View Controller使用指定的类来调用,ScrollingViewController如下所示:

class ScrollingViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView! //outlet for the Scroll View

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1) Create the two views used in the swipe container view
        var storyboard = UIStoryboard(name: "App", bundle: nil)
        var subOne: SubProfileOneViewController = storyboard.instantiateViewControllerWithIdentifier("subone") as! SubProfileOneViewController
        var subTwo: SubProfileTwoViewController = storyboard.instantiateViewControllerWithIdentifier("subtwo") as! SubProfileTwoViewController

        // 2) Add in each view to the container view hierarchy
        //    Add them in opposite order since the view hierarchy is a stack
        self.addChildViewController(subTwo);
        self.scrollView!.addSubview(subTwo.view);
        subTwo.didMoveToParentViewController(self);

        self.addChildViewController(subOne);
        self.scrollView!.addSubview(subOne.view);
        subOne.didMoveToParentViewController(self);

        // 3) Set up the frames of the view controllers to align
        //    with each other inside the container view
        var adminFrame :CGRect = subOne.view.frame;
        adminFrame.origin.x = adminFrame.width;
        subTwo.view.frame = adminFrame;

        // 4) Finally set the size of the scroll view that contains the frames
        var scrollWidth: CGFloat  = 2 * self.view.frame.width
        var scrollHeight: CGFloat  = 262
        self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,什么情况是,那些使用类中的两个视图控制器SubProfileOneViewControllerSubProfileTwoViewController分别,被实例化subOnesubTwo.然后将这些作为子项添加到Scroll View中,以创建一个界面,用户可以向右滑动以访问另一个视图(几乎像Snapchat).subOne并且subTwo应该是并排的,用户应该能够从一个滚动到下一个,反之亦然.

以下是我的故事板上的所有内容: 在此输入图像描述

SubProfileOneViewController并且SubProfileTwoViewController每个都有一个视图(分别用绿色和红色表示),每个都有相同的精确约束:高度= 262,超级视图的尾随空间= 0,超级视图的前导空间= 0,超级视图的顶部空间= 0

理想情况下,在运行时,应该有两个视图,一个绿色和一个红色,用户应该能够在每个视图之间滑动.但是,这是实际发生的事情: 在此输入图像描述

绿色和红色视图不会占据整个屏幕宽度,而是会在左侧压缩成一个小条子,而大多数视图控制器都是白色而不是各自的颜色.我尝试了很多东西,但我不确定我做错了什么.

(代码的功劳ScollingViewController归到github上的lbrendanl,链接到这里:https://github.com/lbrendanl/SwiftSwipeView)

Swi*_*ect 2

注意: 我建议使用一个Autolayout解决方案,它将自动处理方向变化、屏幕尺寸,所有这些都比我下面开发的解决方案代码少得多,解决方案更强大

使用当前的方法,使用嵌入 UIViewControllers,您必须等待这些控制器完成各自的初始化,这与您的布局直接竞争。 请注意,您将需要在方向改变时重新计算位置+尺寸。同样,虽然这可行,但它不是一个健全的设计,因为它需要大量代码、逻辑和一般功能,您可以使用NSLayoutConstraint.

测试、构建、链接和运行):

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // 3) Set up the frames of the view controllers to align
    //    with each other inside the container view
    var adminFrame = self.view.frame
    subOne.view.frame = adminFrame
    adminFrame.offset(dx: self.view.frame.width, dy: 0)
    subTwo.view.frame = adminFrame;

    // 4) Finally set the size of the scroll view that contains the frames
    let scrollWidth = 2 * self.view.frame.width
    self.scrollView!.contentSize = CGSizeMake(scrollWidth, self.view.frame.height)
}
Run Code Online (Sandbox Code Playgroud)

这是滚动(正在运行)。请注意,我创建了ViewLayoutAssistant.swift类的视图,它允许您非常方便地可视化位置和比例。

在此输入图像描述

您也不需要告诉视图其层次结构已更改。最后一段代码应该可以帮助您继续:

@IBOutlet weak var scrollView: UIScrollView!
weak var subOne: SubProfileOneViewController!
weak var subTwo: SubProfileTwoViewController!

    self.addChildViewController(subTwo);
    self.scrollView!.addSubview(subTwo.view);
    self.addChildViewController(subOne);
    self.scrollView!.addSubview(subOne.view);
Run Code Online (Sandbox Code Playgroud)