【问题标题】:Swift - UIView when done updating its frame?Swift - UIView 完成更新其框架时?
【发布时间】:2017-05-02 08:17:11
【问题描述】:

我有一个UITableViewCell,我在其中添加了我的自定义视图,比如说

class MyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    convenience required init(size: CGFloat) {
        self.init(frame: CGRect.zero)

        height = size

        // init my other sub contents [1]
    }
}

来自我的UITableViewCell

let myView = MyView(size: 35)
contentView.addSubView(myView)

我的问题:我在 MyView 中的其他 UI 组件依赖于 MyViewwidthheight,现在设置它们在 [1] 中的位置还为时过早,因为 MyView 的框架现在是 @ 987654331@

我应该将我的其他 UI 组件放在 MyView 的什么位置? UIView 中是否有代表告诉我:“嘿,视图的框架已更新为其实际大小。”? (如viewDidAppear 中的UIViewController

问候,

【问题讨论】:

    标签: swift uiview delegates


    【解决方案1】:

    你可以的

    override var frame: NSRect {
        didSet {
            //update your subviews
        }
    }
    

    在您的MyView 课程中。但实际上,您应该只考虑设置适当的约束:

    https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/

    【讨论】:

    • 我在didSet 中打印出frame 的值,发现它只被调用了一次,其值为:(0.0, 0.0, 0.0, 0.0)。这不是我期望的大小。
    • 那是因为你没有在某个时候自己设置它。您是在暗示框架稍后会在某处发生更改 - 我已经为您提供了通知该更改的代码,但您需要确保做出更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    相关资源
    最近更新 更多