Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting iOS 8 Orientation change in UIView

Tags:

ios

autolayout

I am detecting Regular and Compact size classes with TraitCollectionDidChange in iOS8.

I am trying to fit a particular set of data horizontally across the screen and it does not fit in Horizontal Class Regular on a Portrait iPad Mini however there would be plenty of space in Landscape.

The problem is that Horizontal SizeClass is Regular in Portrait and Landscape so the TraitCollectionDidChange does not fire and I don't get told about the Orientation change.

I have heard reference to viewWillTransitionToSize but that is not available in a UIView.

Are Apple saying they do not want UI layout diffs from Horizontal Class Regular - Portrait and Horizontal Class Regular - Landscape?

If it's OK to do it how do I get told, in a UIView, about the Orientation change?

like image 905
Pat Long - Munkii Yebee Avatar asked Apr 29 '15 14:04

Pat Long - Munkii Yebee


People also ask

Which method called when orientation changes iOS?

This inherited UIViewController method which can be overridden will be trigger every time the interface orientation will be change.

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

What is device orientation in Xcode?

You can configure which device orientation does your iOS app support in the Xcode target —> General —> Deployment Info section also. Just check the checkbox in the Deployment Info —> Device Orientation section. Below is the explanation of each orientation meaning, you can check multiple checkboxes.


1 Answers

Just add the method -(void) layoutSubviews into your UIView subclass. It will get called any time the view's dimensions change. Don't forget to call [super layoutSubviews] inside your new method.

Then you can check self.traitCollection.verticalSizeClass and self.traitCollection.horizontalSizeClass to see what you're looking at.

-(void) layoutSubviews
{
    [super layoutSubviews];

    //   your code here...
}
like image 97
amergin Avatar answered Sep 23 '22 14:09

amergin