Reputation: 1999
I'm very new to Swift and trying to make a simple application to learn. I'm trying to setup on screen a UISlider and set it in the middle of the screen, the problem is that the view is probably isn't ready yet. I tried to do something like this:
> private let bullsEyeSlider: UISlider = {
> let slider = UISlider(frame: CGRect(x: self.view.frame.midX,y:100, width: 500, height: 20))
> slider.minimumValue = 0
> slider.maximumValue = 100
> return slider
> }()
to get the center of the view (the x center at least) but of course it can't work. I'm trying to think of something else to do but other than just setting up the slider after the view is initialised I can't think of anything else. Tried to search the web but with no luck.
Upvotes: 0
Views: 19
Reputation: 2269
In viewDidLayoutSubviews method of the view controller add:
bullsEyeSlider.center = view.center
Upvotes: 1