I have a UIViewScroll
(background color is blue) in view controller. I need a UIView
(background color is white) that were from Xib. The Xib view has a UILabel
(background color is green) with constraints. Now, the problem is UILabel
constraints not applied after adding it to scrollView
. How to add UIView
without loss of constraints? Refer following screenshots and code.
Note:
I need to just update constraints of the sub views of the UIView
without using IBOutlets
of NSLayoutConstraints
.
UIView
on Xib:
Code:
class ViewController: UIViewController {
@IBOutlet var scrollView: UIScrollView!
var profileView:UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
self.scrollView.addSubview(self.profileView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.profileView.layer.frame.size = CGSize(width: self.scrollView.frame.width, height: self.profileView.frame.height)
self.profileView.layer.position = CGPoint(x: self.scrollView.frame.width/2, y: (self.profileView.frame.height/2)+10)
}
}
Output:
Update: More Information
I am aware to set contentSize
of the scrollView. I used layer
properties of UIView
for manipulating height and width of the UIView
. Instead of changing height and width also I need to update constraints of the sub views of UIView
.
This is an example for understanding. But, In real I will be add more views like that.
Github Repository : https://github.com/RAJAMOHAN-S/ScrollViewTest
Required output: