【问题标题】:Custom UIView - autolayout not applied自定义 UIView - 未应用自动布局
【发布时间】:2016-02-26 17:36:11
【问题描述】:

我的注册场景在场景中有一个自定义 UIView。根据以下屏幕应用了自动布局约束。黄色表示自定义视图。

自定义视图在身份检查器中设置为RegisterView。 RegisterView 类只加载一个 XIB:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    NSBundle.mainBundle().loadNibNamed("RegisterUserView", owner: self, options: nil)
}

XIB 应用了自动布局,因此呈现如下:

但是,当我运行应用程序时,似乎没有应用自动布局约束:

我是否需要添加代码来强制应用布局约束?

【问题讨论】:

  • 您已经对自定义视图的子视图应用了约束,但是当您将此视图添加到另一个视图时,可以说您的 ViewController 的视图,那么您还需要对这个 customView 对象应用约束,这样我可以相对于它的父视图进行布局。

标签: ios uiview autolayout xib


【解决方案1】:

我尝试了类似下面的方法并且成功了

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

        self.customSubView = CustomView1.loadFromNibNamed("CustomView") as! CustomView1

        customSubView.frame = _CustomSubView.frame 
        customSubView.frame.origin = CGPoint(x: 0, y: 0)
        self._CustomSubView.addSubview(customSubView)
    }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多