【问题标题】:UIView Subclass Set Own Height + Constraints In UIViewController + Storyboard in SwiftUIView 子类在 Swift 中设置自己的高度 + UIViewController 中的约束 + 情节提要
【发布时间】:2017-04-02 00:53:54
【问题描述】:

我有一个 UIViewController 子类,其中包含一个 UITableView、一个名为 ICYCollapsableInputView 的 UIView 子类和另一个名为 ICYHeaderVIew 的 UIView 子类。

ICYHeaderView 是屏幕截图中显示的四舍五入的 UIView。它包含一个圆形按钮 - 带有绿色“添加”(+) 按钮的按钮。

ICYCollapsableInputView 是一个视图,现在将有一个 UITextField 和一个保存按钮。我希望能够通过调用 ICYCollapsableInputView 的适当函数从 UIViewController 展开和折叠此视图。

当用户点击 ICYHeaderView 上的圆形按钮时,ICYCollapsableInputView 应该会在高度上展开,向下推 UITableView。我有 ICYCollapsableInputView 展开和折叠,但我不知道如何让 UITableView 响应更改。 (见动画)

我用红色背景为 xib 中的主视图着色,我在其中添加的视图用蓝色背景着色(见屏幕截图)

我可以使用自己的高度约束来调整 ICYCollapsableInputView(如代码和动画中所示)的大小,但 UITableView 约束似乎忽略了它。

这是来自 ICYCollapsableInputView 的代码:

class ICYCollapsableInputView: UIView {

var view: UIView!

@IBOutlet weak var heightConstraint: NSLayoutConstraint!

@IBInspectable public var collapsedHeight: CGFloat = 150 {
    didSet {
        self.heightConstraint.constant = collapsedHeight
    }
}
@IBInspectable public var expandedHeight: CGFloat = 250 {
    didSet {
        self.heightConstraint.constant = expandedHeight
    }
}

// MARK: - Init

func loadFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "ICYCollapsableInputView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
}


override init(frame: CGRect) {
    // 1. setup any properties here

    // 2. call super.init(frame:)
    super.init(frame: frame)
    // 3. Setup view from .xib file
    xibSetup()

}

required init?(coder aDecoder: NSCoder) {
    // 1. setup any properties here

    // 2. call super.init(coder:)
    super.init(coder: aDecoder)
    // 3. Setup view from .xib file
    xibSetup()
}


func xibSetup() {
    view = loadFromNib()
    view.frame = bounds
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    addSubview(view)
    self.heightConstraint.constant = self.collapsedHeight
    var rect = self.frame
    rect.size.height = self.collapsedHeight
    self.frame = rect
    self.view.frame = rect

}

func revealInputView() {
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
        var rect = self.frame
        rect.size.height = self.expandedHeight
        self.frame = rect
        self.heightConstraint.constant = self.expandedHeight
        self.view.layoutIfNeeded()
    }, completion: nil)
}

func closeInputView() {
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
        self.heightConstraint.constant = self.collapsedHeight
        var rect = self.frame
        rect.size.height = self.collapsedHeight
        self.frame = rect
        self.view.layoutIfNeeded()
    }, completion: nil)

}

 }

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题。发生这种情况是因为视图正在改变自己的高度吗?

标签: ios swift uitableview uiview subclass


【解决方案1】:

您只需要更改高度限制即可。

    func revealInputView() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {

            self.heightConstraint.constant = self.expandedHeight //or 250
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

    func closeInputView() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {

            self.heightConstraint.constant = self.collapsedHeight //or 150        
self.view.layoutIfNeeded()
        }, completion: nil)
}

故事板中的约束应该遵循

用于输入大众

superview 的尾随空间 - 0 ,superview 的前导空间 - 0 ,superview 的顶部空间 - 0 ,tableview 的底部空间 - 0

表视图

inputVw 的顶部空间 - 0,superview 的前导空间 - 0,superview 的尾随空间 - 0,superview 的底部空间 - 0

【讨论】:

  • 我试过了。除了 ICYCollapsableInputView 的主框架(红色部分)没有改变之外,它做同样的事情。
  • 在图像 3 中 - 在约束中显示 4 of 4。高度约束在哪里??
  • 我在 ICYCollapsableInputView xib 上设置了高度约束。您可以在图 2 中看到它。
  • 我想补充一点,我知道我可以在视图控制器上设置一个高度约束(有效),但我想封装 ICYCollapsableInputView 并让它根据我添加的子视图设置自己的高度对它 - 如果这可能的话。
猜你喜欢
  • 2016-08-02
  • 2020-02-29
  • 2022-01-22
  • 2016-05-20
  • 2014-01-30
  • 2016-11-16
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
相关资源
最近更新 更多