I am making an ios App and is relying on AutoLayout constraints to set the sizes of views and subviews layout.
I made a custom subclass of an UIScrollView
, see below. When I write its init
method, I see its super class only has init(frame:)
to override. But because I am using autolayout, I am not supposed to set the frame.
So how should I write the init
method in my case? Shall I pass in a dummy frame
value:
MyCollectionView: UIScrollView {
init() {
super.init(CGRect(x:0, y:0, width:0, height:0))
}
}
Usage:
ViewController:UIViewController {
let myScrollView = MyScrollView()
}
You don't have to worry about setting/not a frame as when you use autolauout
let myScrollView = MyScrollView()
myScrollView.translatesAutoresizingMaskIntoConstraints = false
the frame will has no effect with translatesAutoresizingMaskIntoConstraints
being set to false
, so do
class MyScrollView: UIScrollView {
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp() {
}
}
let gg = MyScrollView(frame: .zero)
gg.translatesAutoresizingMaskIntoConstraints = false
// add constraints
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.