I have a UIView called containerView
which contains all the UI elements in my view. The reason why I am making the containerView
in the first place is because I would like to implement keyboard support for devices with smaller screen estate and I would not be able to embed self.view
into the UISCrollView .
When I set the constraints on my containerView
, I do it like this:
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
containerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
containerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
containerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
/* constraints of containerView's subviews */
}
Since I am constraining all four anchors of my containerView
to view
, I am curious to know if there is a less verbose way (maybe a one-liner?) that will achieve this.
Using built in methods, I believe this would be the shortest way to do it.
let views = ["contentView": contentView]
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "|-[contentView]-|", views: views))
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[contentView]-|", views: views))
But is this is something you do often, creating an extension you can call would be a better option.
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.