StackView约束行为在NIB/XIB中有何不同?

问题描述:

我试图在迅速iOS的10StackView约束行为在NIB/XIB中有何不同?

当我创建一个堆栈视图中,并在其2个对象来创建在厦门国际银行/笔尖文件可重复使用的意见,并确保我stackview被约束的容器视图nib/xib(从上到下,从下到上,导致前导,最后跟踪),我得到一个错误,说我错过了第一个和第二个对象的Y位置。只创建其中一个通常可以修复它。虽然这是事情横向的地方。在我之前的所有研究中,如果我将我的堆栈视图分配给Fill,则不需要执行此操作。尽管这似乎让Xcode安静下来,但当我尝试运行我的程序时,它会产生一个过度约束的问题。

这里是我以前从我的主要情节串连图板载入我的观点笔尖:

public protocol NibOwnerLoadable: class { 
    static var nib: UINib { get } 
} 

//MARK:- Generic Implementation 
public extension NibOwnerLoadable { 
    // Use the xib file with the same name as your UIView subclass located in the bundle of that class 
    static var nib: UINib { 
     return UINib(nibName: String(describing: self), bundle: Bundle(for: self)) 
    } 
} 

//MARK:- Support for instation from the XIB file 
public extension NibOwnerLoadable where Self: UIView { 
    // Function to load content and constraints automatically 
    func loadNibContent() { 
     let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing] 
     for view in Self.nib.instantiate(withOwner: self, options: nil) { 
      if let view = view as? UIView { 
       view.translatesAutoresizingMaskIntoConstraints = false 
       view.frame = bounds 
       self.addSubview(view) 
       layoutAttributes.forEach{ attribute in self.addConstraint(NSLayoutConstraint(item: view, attribute: attribute, relatedBy: .equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0.0)) 
       } 
      } 
     } 
    } 
} 

我知道我的问题是自动布局,但原因和位置添加一个额外的约束。这个问题与我想要做的非常接近,但由于某种原因,我的AutoLayout知识在我的脑海中混乱了。 Adding a subclassed UIView to a Nib with its auto layout constraints 有一点帮助,将不胜感激。请解释为什么,而不仅仅是如何去做。我是一个喜欢理解背后故事的人。它使它保持逻辑并更容易。

这里是我的filterView作为一个nib/xib的图片,在searchAndFilterView中加载为FilterView(UIView),它被加载到我的视图控制器中的一个视图板中。考虑可重用的工具视图。

Storyboard ViewController

FilterView XIB

SearchAndFilterView XIB

+0

让我澄清更多... –

+0

让我们先看看我能否先完成这个任务。我希望我的XIB/NIB视图的容器视图具有一定的高度。这些XIB/NIB视图必须具有一定的大小,并且必须处于堆栈视图中,以便在此容器视图隐藏时,可以缩小视图的动画效果。我可以在没有XIB/NIB的情况下做到这一点,但我无法使用我的XIB/NIB fileOwner的子集视图来完成此操作。 –

Woohou!

这是我如何解决这个问题。

我意识到使用XIB/NIB对我的视图进行高度约束是设置视图时不需要隐藏的。但是当我将.isHidden设置为真值时,这与我的身高限制相冲突。

我需要让Autolayout在视图从隐藏变为隐藏时进行自动布局,反之亦然。通过设置优先级默认值为1000的约束条件,我告诉Autolayout必须确保此约束始终处于活动状态。

我意识到,通过将优先级设置为999,我告诉Autolayout它可以覆盖我的约束,当它真的需要这样做。为什么999,我认为当视图隐藏时,它确实没有大小,这对Autolayout来说是非常高的优先级。

我希望我知道这一点,或者知道如何找出默认的自动布局优先级。

如果有人知道更多关于此,我将不胜感激更多信息或链接!