I have a UIView being added to a UIViewController, I want to ensure the views have been fully loaded as I want to access and use the frames of these views. I am working within the UIView and the ViewController is just for testing purposes.
Here is the example I am working with:
I am loading a custom UIView into a UIViewController so am initialising the view in the following way:
In the UIViewController
var slideView: BSlideBar = BSlideBar(frame: CGRect(x: 0, y: 150, width: 320, height: 54))
self.view.addSubview(slideView)
In the UIView
override init(frame: CGRect) {
super.init(frame: frame)
customInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
customInit()
}
public func customInit() {
Bundle.main.loadNibNamed("BSlideBar", owner: self, options: nil)
self.addSubview(self.slideView)
self.slideView.frame = self.bounds
}
I have an XIB file associated with my UIView so am loading the Nib in the custom init function.
This UIView has other UIViews inside and is all being added to the UIViewController. This is all working fine, the issue comes when I try to access the attributes of those views:
Example:
draggableView.layer.cornerRadius = draggableView.frame.width/2
This will make the draggableView a circle and also allows me to base the size of the draggableView on the view it is contained in while maintaining it as a circle. This means my view will stay consistent whatever frame I initialise it with. Annoyingly the frame is always returned as the frame in the XIB file instead of the frame of the loaded UIView. (if I click a button after it has loaded it then returns the expected and desired frame)
Example 2:
self.centreLine.frame = CGRect(x: Int(sidePadding), y: Int(slideView.frame.height/2), width: Int(UIScreen.main.bounds.width - 2 * sidePadding), height: 2)
Once again I want this view to adjust to the size of the view it is contained in. In this case it should stay within the padding of the edges and be exactly in the middle of the view. Once again this isn't the case.
I have tried to use this answer by using:
-(void)awakeFromNib
override func layoutSubviews() {
super.layoutSubviews()
}
But neither of these seem to be called late enough.
The problem is partly due to being inside a UIView as I would normally called something like this:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
I am slightly confused but really interested in finding out why this problem is occurring. The main problem I have is with concisely searching for the problem and have exhausted all my current leads.