Best Approach to Change Xib Direction Programmatically

Best way to change UIViewController view NIB programmatically

You shouldn't switch nibs, you should switch controllers.

See this answer for: Easiest Way to Support Multiple Orientations

Alternative iOS layouts for portrait and landscape using just one .xib file

A way to do this is to have three views in your .xib file. The first one is the normal view of your Viewcontroller with no subviews.

Then you create the views for portrait and landscape as you need them. All three have to be root-level views (have a look at the screenshot)

Sample Image

In your Viewcontroller, create 2 IBOutlets, one for the portrait and one for the landscape view and connect them with the corresponding views in the interface builder:

IBOutlet UIView *_portraitView;
IBOutlet UIView *_landscapeView;
UIView *_currentView;

The third view, _currentView is needed to keep track of which one of these views is currently being displayed.
Then create a new function like this:

-(void)setUpViewForOrientation:(UIInterfaceOrientation)orientation
{
[_currentView removeFromSuperview];
if(UIInterfaceOrientationIsLandscape(orientation))
{
[self.view addSubview:_landscapeView];
_currentView = _landscapeView;
}
else
{
[self.view addSubview:_portraitView];
_currentView = _portraitView;
}
}

You will need to call this function from two different places, first for initialization:

-(void)viewDidLoad
{
[super viewDidLoad];
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
[self setUpViewForOrientation:interfaceOrientation];
}

And second for orientation changes:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setUpViewForOrientation:toInterfaceOrientation];
}

Hope that helps you!

Autoresizing masks programmatically vs Interface Builder / xib / nib

Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). Your cited "scenarios" are correct.

Handle orientation change on iPad with one UIViewController and two XIB

I'm not sure there are any strange side-effects with this implementation, but try something like this and see if it works for you:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(orientation)) {
[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.transform = CGAffineTransformMakeRotation(M_PI);
}
} else if (UIInterfaceOrientationIsLandscape(orientation)){
[[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
} else {
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
}
}

This assumes that the File's Owner in your MenuView and MenuViewLandscape XIBs are both set to MenuViewController and that the view outlet is set in both XIBs as well. All of your outlets should be reconnected properly on rotation when using loadNibNamed.

If you are building for iOS 4, you could also replace the loadNibNamed lines with these:

UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;

and

UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

These assume that the UIView that you want to display immediately follows the File's Owner and First Responder proxy objects in the XIBs.

Then you just need to make sure the views are rotated properly for the interface orientation. For all of the views that are not in the default portrait orientation, rotate them by setting the transform property of the view and using CGAffineTransformMakeRotation() with the appropriate values as shown in the example above.

The rotation alone might solve your issue without the extra loading of the NIBs. However, loading a whole new instance of a MenuViewController and setting its view to the existing MenuViewController's view might cause some strange behavior with lifecycle and rotation events, so you might be safer trying the examples above. They also save you the trouble of having to create new MenuViewController instances when you only need the view from it.

Hope this helps!

Justin

Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

You have to load one view, then check orientation and load another if needed. You check orientation in shouldAutorotateToInterfaceOrientation: returning yes if you want to rotate.

I use a navigation controller to manage the transition. If I have the portrait view up and the device rotates, I push the landscape view and then pop the landscape view when it return to portrait.

Edit:

I return YES for all orientations in
shouldAutorotateToInterfaceOrientation:
but will this be called when the app
launches? Do you push your view inside
of this function?

The orientation constants are not globals you query but rather part of the messages sent the controller by the system. As such, you cannot easily detect orientation before a view controller loads. Instead, you hardwire the app to start in a particular orientation (usually portrait) and then immediately rotate. (See mobile Safari. It always starts in portrait and then rotates to landscape.)

These are the two methods I used to swap out my portrait and landscape views.

All three view controllers have this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

The portrait has this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
[self.nav pushViewController:rightLVC animated:NO];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
[self.nav pushViewController:leftLVC animated:NO];
}
}

Each landscape controller has this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
[self.nav popViewControllerAnimated:NO];
}

The app starts in portrait. If the orientation of the device is landscape, it pushes the appropriate landscapes. When the device rotates back to portrait, it pops the landscape. To the user it looks like the same view reorganizing itself for a different orientation.

XIB with dynamic width, fix height in center - programmatically constraints seems not to work

You are mixing up auto layout and fixed placement (with autoresizing mask). What you want to do is completely use auto layout so that the view will adjust its layout automatically. You say you want a horizontal distance of 20, a fixed height and to be centred so I would do this:

if let alertView = Bundle.main.loadNibNamed(Constants.XIB.titleImageLabelThreeButtonsAlertView, owner: self, options: nil)?.first as? TitleImageLabelThreeButtonsAlertView {

view.addSubview(alertView)

// Start using auto layout
alertView.translatesAutoresizingMaskIntoConstraints = false

// Set the leading and trailing constraints for horizontal placement
view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: -20))
view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 20))

// Centre it vertically
view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0))

// Set the fixed height constraint
let fixedHeight: CGFloat = 100
view.addConstraint(NSLayoutConstraint(item: alertView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 0, constant: fixedHeight))
}

That will get you what you want no matter how the device, superview, orientation, etc changes.



Related Topics



Leave a reply



Submit