user1940829
user1940829

Reputation: 1

Can we use same Xib (which contains UIView) to support both landscape and portrait orientations

I have a ViewController (searchListViewController) in my storyboard. This ViewController is embedded in a navigationController. The ViewController in storyboard rotates properly when orientation changes.

I have an XIB which contains an UIView (CustomAlertView). I want to present this CustomAlertView from my searchListViewController.

To do this, i have included the following code in my searchListViewController class.

searchListViewController.m:

-(void)loadCustomAlertView
{

     NSArray *allviews = [[NSBundle mainBundle] loadNibNamed:@"CustomAlertView" owner:self options:nil];
     CustomAlertView *customAlertView_Obj = allviews[0];
    [self.navigationController.view addSubview:customAlertView_Obj];
}

The Problem is: the parent navigation controller rotates properly, but the child "CustomAlertView" does not rotate when the orientation changes. Am i doing anything wrong in this code?

Is there anyway to handle rotations for an XIB file programmatically?

I want to use the same CustomAlertView XIB for both Portrait and Landscape. Is it possible?

Upvotes: 0

Views: 181

Answers (2)

Sunil Targe
Sunil Targe

Reputation: 7459

Simply you could do addSubview:customAlertView_Obj on searchListViewController instead of adding on navigationController.

Hope, it'll work.

Upvotes: 0

Salman Zaidi
Salman Zaidi

Reputation: 9842

YES, you can use same xib file for both orientations.

Using Auto-resizing/auto-repositioning constraints (in "Size Inspector Tab") and auto-layouts(iOS6 and above), define properties of your objects so that when your screen orientation changes, all the views inside your parent view get aligned and/or repositioned, resized.

You can view how your view looks in different orientations by changing orientation from "Attributes Inspector Tab".

Upvotes: 1

Related Questions