So at work I came across a problem with using a subclass view in order to display a customized button.
If I understand correctly, using the storyboards means that the customized view first initiates the view through init(coder) which is not how I wanted the view initiated.
So I added a view through code instead and everything worked much better. Without going into the debate of to Storyboard or not to Storyboard, in this case it seemed much simpler.
When I create the frame for my view, though, I want it to be proportional to whatever the user’s device’s screensize might be. So when to I create it, and set the frame size to be proportional to a view (map view in my case)? We have to wait till the views have all been instantiated by the device. And that’s where LayoutSubviews comes in. Depending on where one sets up their subview, the method is different. If the developer is adding the subview in a UIView then the method is func layoutSubviews. If the subview is added in a ViewController then the method is func viewDidLayoutSubviews.
These methods wait for the views and subviews to be calculated and layed out by the device before doing whatever action is required. In my case, since I wanted my subview to be placed in a proportionate space to the the map view (in it’s lower left side as it’s origin point) I needed the OS to calculate the super view size for that device and plug that into the frame size of my subview. viewDidLayoutSubviews did the trick.
let originPoint: CGPoint = CGPoint(x: mapView.width–70,y: mapView.height–85)
let customView: UIView = UIView(frame: CGRect(origin: originPoint, size: CGSize(width: 50, height: 50)))
Warning – as always, make sure to call the super – super.viewDidLayoutSubviews.