开发者

Autoresizing subviews in iPhone programmatically

I have created a UIViewController and a UIView with two UIButtons in it.

self.view = [[UIView alloc]initWithFrame:CGRectMake( 0,0,320,480)];
self.view.autoresizesSubviews = YES;

self.view.backgroundColor = [UIColor redColor];
UIView *view;
view = [[UIVew alloc]initWithFrame:CGRectMake(0,0,320,480)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIButton *button1;
button.frame = CGRectMake(100,130,120,50);
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[controller addSubview:view];

// […]

- (void)buttonAction:(id)sender
{
    // I have created another view for a purpose
    [view removeFromSuperview];
    UIView *view_obj;
    view_obj.backgroundColor = [UIColor greenColor];
    view_obj.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:开发者_如何学Goview_obj];  
}

Problem: When I change the orientation into landscape the view which has the button is resized but when i click on the button the view with the blueColor is displayed without autoresized. Where i can see half as root view in redColor and half as the subview in greenColor.I have also overrided the ShouldAutoRotate method and done this project programmatically. Please do reply me.


In Interface Builder, select the view object and then in the inspector window's "View Attributes" pane make sure you have selected a status bar and a "Top Bar" of "Navigation Bar" (assuming your app has a navigation bar). You'll see these near the top in the "Simulated User Interface Elements" section. This informs the app to always leave room for these when determining the size of UIViewController's view.


Try to study this question.

Or

See following code From this question.

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {    
    UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
    if (interfaceOrientation == UIInterfaceOrientationPortrait 
        || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        button1.frame = CGRectMake(100, 33, 226, 275);
        button2.frame = CGRectMake(440, 33, 226, 275);
        button3.frame = CGRectMake(100, 360, 226, 275);
        button4.frame = CGRectMake(440, 360, 226, 275);
        button5.frame = CGRectMake(100, 693, 226, 275);
        button6.frame = CGRectMake(440, 693, 226, 275);
    }
    else 
    {
        button1.frame = CGRectMake(100, 50, 226, 275);
        button2.frame = CGRectMake(400, 50, 226, 275);
        button3.frame = CGRectMake(700, 50, 226, 275);
        button4.frame = CGRectMake(100, 400, 226, 275);
        button5.frame = CGRectMake(400, 400, 226, 275);
        button6.frame = CGRectMake(700, 400, 226, 275);
    }
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation || UIInterfaceOrientationPortraitUpsideDown);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜