Reputation: 1263
I have an app up and running which uses old iOS xib design methods of springs and struts. Each screen contains two xib files for iPhone 3.5 inch and iPhone 4 inch, all programmatic animations are slightly generically made such that they work on both resolutions.
Since the introduction of iPhone6 and iPhone6 Plus phones this all has kind of failed such that app views were appearing very small on these devices. I have used a work-around to make sure that the app scales up on 6 and 6Plus by removing 'required' icon file for 6 and 6 plus resolutions which tells iOS that this app is made to support native resolution on these devices.
My question is When I tried switching all views to auto-layout and size classes (spent like 4 days struggling) every time something failed on the views. Some images only scale up in height, some hug to one side, some scrollviews show images inside them on one side only etc So I reverted to using the same technique of struts and springs.
Do I really need to shift my views to Auto-layout? is there a way to avoid this or guidelines so that I may not need it?
If there is no way out of it, How do people usually make animations in Auto-Layout views? All animations are translation animations, previously both devices had 320 width which made it easier, how do we now handle different resolutions and make generic translation animations?
Upvotes: 0
Views: 237
Reputation: 534893
No one is holding a gun to your head to use Autolayout. There are now a whole lot of possible screen sizes so the idea is that Autolayout can help your interface adapt to different sizes. If you don't want to use it, don't use it.
I don't understand the second question, plus you're using "resolution" in a funny way. It's not about resolutions, it's about sizes, plain and simple. The issue is the same regardless of whether you're using Autolayout or not. If you want to animate from the left side of the screen to the right side of the screen, naturally if the screen is wider, you have to go further. But you know the size of the screen, so there's no problem. You just do a little basic arithmetic.
And if you're using Autolayout, you don't even need arithmetic; you position things relatively using Autolayout and they work on any size screen. That's the whole point.
For example, here's code that does actually animate moving a view from the left side of the screen to the right side, and it works regardless of the size of the screen!
let c = self.leftConstraint.constant
NSLayoutConstraint.deactivateConstraints([self.leftConstraint])
let rightConstraint = NSLayoutConstraint(
item: v, attribute: .Trailing, relatedBy: .Equal, toItem: self.view,
attribute: .TrailingMargin, multiplier: 1, constant: -c)
NSLayoutConstraint.activateConstraints([rightConstraint])
UIView.animateWithDuration(0.4, animations: {self.view.layoutIfNeeded()})
Upvotes: 2
Reputation: 13313
You don't have to switch to AutoLayout, but the sooner you do, the easier things will be for you long term. Who knows what new size screens we may see in the future.
You can animate views when using AutoLayout, you just do it slightly differently. Instead of changing the frame rects, you change the constraints that are affecting a view. This can be done by changing the constants on existing constraints, changing priorities (note you can not change a priority that is set to 1000), or by adding/removing constraints.
The steps to animate these changes are:
1) Call layoutIfNeeded
on a parent view to ensure everything is already in the correct place.
2) Make the changes you need to your constraints, constants, priorities etc.
3) Call layoutIfNeeded
again, this time inside UIView
's animateWithDuration
method.
Upvotes: 3
Reputation: 1150
You could probably get away with it by creating a couple more xib files for each screen size, and then figure out what the screen size is and load the proper xib file. But that would be even more trouble! Especially that You have to keep doing that later on for every new screen size Apple introduces. So, go with AutoLayout, read THIS tutorial and you'll be good to go! I haven't had a case in which I couldn't use AutoLayout. It's very powerful.
You can animate constraints! If you ctrl+drag from a constraint (those blue lines) into your code, you can hold a pointer to the NSLayoutConstraint
object, and then simply change the .constant
value in a [UIView animate...
block.
Upvotes: 2