开发者

UIView and orientation changes: UITextField Preservation and UI frame custom animations

开发者 https://www.devze.com 2023-01-07 21:16 出处:网络
With no developer interaction you can have a view resize itself to support an orientation, in many cases this is fine, but what if you need to change the layout of your UI objects to better suit the n

With no developer interaction you can have a view resize itself to support an orientation, in many cases this is fine, but what if you need to change the layout of your UI objects to better suit the new orientation?

Apples answer is to:

  • Have two separate ViewControllers with xibs that reflect the desired layout.
  • Designate a master ViewController, (recommended portrait).
  • Subscribe to beginGeneratingDeviceOrientationNotifications
  • In the notification event method (master), check the orientation and either push or pop the slave ViewController as a modal.

At face value, if the layout contains a number of UITextFields, changing the orientation clears the fields (from the users point of view) suggesting that a field copy method is also needed in the notification event method. Unless there is an automated provision for this? (or an alternative pattern)

But what if you want to have some nice short-delayed UI frame tran开发者_如何学Goslation animations?

Alternatively, rather than having two View controllers I have used CGAffineTransforms to animate UI object frame changes (triggered on willRotateToInterfaceOrientation) with the added bonus of looking sexy, but the code doesn't look very elegant.

Im unaware of any other solutions.

To get to the point: What is the most efficient and fastest, way to change UI object layouts on orientation events that preferably facilitates frame translation animation?


The most efficient way is to do the layout in code. The downsides are that you have to write ugly code and avoid IB. The upshots are elasticity and performance. I wrote a method in my view controller which accepts UIInterfaceOrientation as an argument and lays out its views accordingly. Then I call this method from -willAnimateRotationToInterfaceOrientation:duration:. Sure, the code is very ugly, but fast. Basically, I just set a bunch of frame properties there.


The transforms are the easiest to integrate with transition animations.

I usually solve this problem by using a different view/view-controller pair for each orientation. The trick to making this work seamlessly is to have the data already entered constantly updated in the external data model object. Each view controller then automatically populates its view's UI from the data model. That way you don't have to worry about moving data between view controllers which can get messy if you have a lot of views.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号