0
votes

In my app i trying create a Interface builder supports both Landscape and portrait in ipad and iPhone.

[In Android we used fill parent to autoresize dynamically created UI-Elements.is there any syntax in iOS to autoresizing]

  1. How to UI-elements create dynamically supports both Landscape mode and portrait mode?

  2. How create the view controller to support the Landscape mode and portrait mode?

  3. Is there required to create a all views and UI-elements dynamically?

3
use autoresizing mask for this or you can go raywenderlich.com/50319/…Gajendra Rawat

3 Answers

2
votes

1)If you will make xib or nib than develop xib or nib in only one mode as portrait or landscape. Than use Autoresizing option as below Image for any control.

Auto layout

http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2. You can use this link for auto layout.

But Auto layout is not work properly as you want. so u need to set frames of control pro grammatically evenif u r using autolayout.

2) And If you want to develop dynamically than using below code you can set frame of all controls. In ViewwillAppear.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];

in viewdidload set your controls as below.

UILabel lbl - [UILabel alloc]init];


-(void)orientationChanged{

       if(Orientation is portrait){
            [lbl setFrame:for portrait];
    }else{
           [lbl setFrame: for landscape];
    }

If device change mode than above notification fire and in that method. you can set frame of control.

I hope you will get your answer.

0
votes

You can use auto - layout for providing both the portrait and landscape mode.

For more details, check this : What is Auto Layout?.

You have to set the constraints for landscape and portrait mode to work. Like if you want a button at the top, you can set constraints on it : from top and left and so on.

If you want a UI element to work change dynamically, you just need to change frame on orientation as per your requirement. Sample code is here :

# pragma mark - Orientation related methods

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(3_0)
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        [self deviceRotatedToLandscapeMode];
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self deviceRotatedToLandscapeMode];
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        [self deviceRotatedToPortraitMode];
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self deviceRotatedToPortraitMode];
    }

}

- (void) deviceRotatedToPortraitMode {

    self.mTableView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);

}

- (void) deviceRotatedToLandscapeMode {

    self.mTableView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.height, self.view.frame.size.height);

}
0
votes

The most reliable approach - Create a method in your view controller -

-(void)setFrameForOrientationChange:(UIDeviceOrientation*) o {
//... 
 implement your code for frame settings..
}