user5336403
user5336403

Reputation:

UIView subclass bounds incorrect in console

Here i have created UIView subclass named as CustomSlider. CustomSlider added on Storyboard and connected with storyboard outlet reference. When i am printing the bounds, I am having issues with printing the bounds, its providing incorrect console output.

 class CustomSlider: UIView {

        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
        }
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.setupView()
        }
        func setupView()  {
            print("self.bounds.height \(self.bounds)" )
            self.backgroundColor = UIColor.red
        }
    }


class ViewController: UIViewController  {
    @IBOutlet weak var slider: CustomSlider?

    override func viewDidLoad() {
        super.viewDidLoad()
        slider?.setupView()
    }
}

When i am trying to build the output bounds displaying as below :

self.bounds.height (0.0, 0.0, 1000.0, 1000.0) 

But i'm setting constraints trailing, top, right, height. I have tried below line also.

slider?.translatesAutoresizingMaskIntoConstraints = false

here is screenshot my project looks like

here is screenshot my project looks like

My expected console output would be the slider bounds.

Upvotes: 2

Views: 450

Answers (1)

matt
matt

Reputation: 535989

Your call to print the bounds comes too soon. A view being loaded from a xib/storyboard has bogus bounds (as shown) until layout has occurred. Layout has not occurred at the time of viewDidLoad; the view is not yet in the interface and no sizes are real yet. Postpone your access to bounds until at least after viewDidLayoutSubviews has been called for the first time.

Upvotes: 2

Related Questions