简体   繁体   English

制作固定大小的UIView,如UISwitch(使用IBDesignable)

[英]Make a fixed size UIView, like UISwitch (using IBDesignable)

When I'm using this guide to create a view that is designed in a XIB, reusable from within a storyboard using the IBDesignable attribute on my UIView subclass, how do I make it have a fixed size, and have its resizing behavior match that of a view like UISwitch? 当我使用本指南创建一个在XIB中设计的视图时,可以使用我的UIView子类上的IBDesignable属性从故事板中重用,如何使其具有固定大小,并使其大小调整行为与像UISwitch这样的观点?

With "resizing behavior" I also mean while designing in interface builder. 在“调整大小行为”时,我也意味着在界面构建器中进行设计。

You could override intrinsicContentSize() in your UIView subclass. 您可以在UIView子类中覆盖intrinsicContentSize() Then you won't need to supply height and width constraints in the interface builder. 然后,您不需要在界面构建器中提供高度和宽度约束。

override var intrinsicContentSize: CGSize {
    return CGSizeMake(width: 100, height: 100)
}

If you only want to supply one of the dimensions, you can use UIView.noIntrinsicMetric instead of a value. 如果您只想提供其中一个维度,则可以使用UIView.noIntrinsicMetric而不是值。

override var intrinsicContentSize: CGSize {
    return CGSizeMake(width: UIView.noIntrinsicMetric, height: 100)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM