Daniel Krom
Daniel Krom

Reputation: 10058

Correct way to handle iOS orientation when not using storyboard

I'm developing an app and I'm not using storyboard (or any xib file). Xib files looks too complicated and sometimes when adding constraints the app crashes with no reason and it feels like a huge mess (even with xCode reset auto constraints) . When I'm writing it with pure code I'm getting much better results and it feels much more natural.

Currently I'm adding view and make them relative by using % (for width only) and it ensures that the view will be 0.5 of the super view width

var someView : UIView = UIView(frame:
            CGRect(
                origin:CGPoint(
                    x: 0,
                    y: otherView.frame.maxY
                ),
                size: CGSize(
                    width: otherView.frame.width * 0.5,
                    height: SOME_HEIGHT_CONST
                )
            )
        )

and it's works good, for example creating a rectangle with 0.8 of super view width: before rotate

The issue is, after rotating: after rotate

What is the correct way to handle orientation when not using storyboard? is it possible to bind views size to orientation changes?

Upvotes: 1

Views: 70

Answers (2)

BonanzaDriver
BonanzaDriver

Reputation: 6452

The "correct" way, as you put it ... or, as I'll call it "best practices" ... would have you using LayoutConstraints, and adjusting the properties on those if needed (versus modifying a property of the frame - such as width - directly).

Depending upon which versions of iOS you're supporting I would recommend you check out the WWDC 2015 videos (specifically those on Auto Layout/LayoutConstraints) and the 2014 videos (updates to TransitionCoordinators, PresentationControllers and ViewControllers). This will arm you with the basics so you will have a better understanding of which functions get invoked during an orientation, content, size or position change.

Upvotes: 2

Hermann Klecker
Hermann Klecker

Reputation: 14068

You are mixing super with superview. super refers to the superclass' method or property. So in your case super.frame.width is the same as self.frame.width as frame.width.

You meant to refer to superview.frame.width, I guess.

Upvotes: 0

Related Questions