X $ C $Ç - 厦门国际银行与汽车和布局(厦门国际银行扩展到任意高度)奇怪的问题 [英] Xcode - Weird issue with xib and auto layout (xib expands to random height)

查看:206
本文介绍了X $ C $Ç - 厦门国际银行与汽车和布局(厦门国际银行扩展到任意高度)奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有厦门国际银行的文件/类问题一个奇怪的自动布局。
我已经创造了斯威夫特一个使用厦门国际银行文件的自定义通知视图(UIView子类)。

I am having a weird auto layout with xib file/class issue. I have created a custom notification view (UIView subclass) in Swift that uses a xib file.


  • 当设备处于纵向上负载时,通知
    是罚款。

  • 当我旋转为横向,再次通知是好的(不过,
    按钮的交互莫名其妙地成了残废/非申请人)

  • 当我旋转回纵向,则厦门国际银行视图(即
    自我)扩展为一个随机的高度(的通知黄
    背景
    ,由设置: self.backgroundColor = UIColor.yellowColor()

  • When the device is in portrait orientation on load, the notification is fine.
  • When I rotate to landscape, again the notification is fine (however, the button interactions somehow become disabled/non-respondent)
  • When I rotate back to portrait orientation, the xib view (i.e. "self") expands to a random height (notice the "yellow" background, that is set by: self.backgroundColor = UIColor.yellowColor()

在我的code没有什么是设置初始增添了视图控制器后的框架或限制。我调整了厦门国际银行文件,我能想到的每一个自动布局约束,并继续有这个问题。

Nothing in my code is setting the frame or constraints after the initial adding to the view controller. I have adjust every auto layout constraint in the xib file I could think of, and continue to have this problem.

下面是一些截图:

Here are some screenshots:

推荐答案

好解决它。
所以,问题是,你不能有自动布局引用设置在IB从厦门国际银行到外部的UIViewController(因为他们不知道彼此,直到您以编程方式添加厦门国际银行作为一个子视图)。所以,你必须以编程方式创建的约束。

Ok solved it. So the problem is, you can't have auto layout references setup in IB from a xib to an external UIViewController (since they don't know about each other until you programmatically add the xib as a subview). So you have to programmatically create the constraints.

下面是我做的:

    // Manual constraints required since view is a xib file being added to an external view controller
    self.setTranslatesAutoresizingMaskIntoConstraints(false)
    var constraintHeight = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 44)
    self.addConstraint(constraintHeight)
    var constraintWidth = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.presentingViewController?.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 1)
    self.presentingViewController?.view.addConstraint(constraintWidth)
    var constraintTop = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: underView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
    self.presentingViewController?.view.addConstraint(constraintTop)

这篇关于X $ C $Ç - 厦门国际银行与汽车和布局(厦门国际银行扩展到任意高度)奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆