如何动态改变窗口导航的主窗口和其他视图的方向?

时间:2011-07-12 04:34:05

标签: iphone xcode uiview uiinterfaceorientation uiwindow

我有一个代码,其中动态创建所有UI项目。我有一个导航控制器,连接到主窗口的其他视图。在这里我有一个问题,当我编写一个方向更改代码,然后它不能自动工作。

编译指示标记MainViewController

@implementation MainViewController

@synthesize imageView; @synthesize btnBegin,btnSite; @synthesize phoneNumber;

 - (void)viewDidLoad {  self.title=@"Midwest Sleep Test";self.view.backgroundColor = [UIColor whiteColor];
CGRect myImageRect = CGRectMake(-10,-10,320,313);
imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView setImage:[UIImage imageNamed:@"midwestsleep.png"]];

[self.view addSubview:imageView];
[imageView release]; 

btnBegin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnBegin addTarget:self action:@selector(Begin:) forControlEvents:UIControlEventTouchUpInside];
[btnBegin setTitle:@"Begin!" forState:UIControlStateNormal];
btnBegin.frame = CGRectMake(112,295,95,45);
[self.view addSubview:btnBegin];

CGRect myLabelRect = CGRectMake(112,345,130,22);
phoneNumber=[[UILabel alloc] initWithFrame:myLabelRect];
phoneNumber.text=@"937-350-5645";
phoneNumber.textColor=[UIColor blueColor];
phoneNumber.font= [UIFont fontWithName:@"ComicSansMsBold" size:18];

[self.view addSubview:phoneNumber];

btnSite = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnSite addTarget:self action:@selector(Site:) forControlEvents:UIControlEventTouchUpInside];
[btnSite setTitle:@"www.midwestsleepmed.com" forState:UIControlStateNormal];
btnSite.frame = CGRectMake(1,370,320,22);

[self.view addSubview:btnSite];

}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation{
return YES;}

enter code-(IBAction)Begin:(id)sender{
SecondView *sV = [[SecondView alloc] init];
[self.navigationController pushViewController:sV animated:YES];
[sV release];}

` - (IBAction为)站点:(ID)发送方{     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“http://www.midwestsleepmed.com”]];}

pragma mark AppDelegate

pragma mark -

@implementation MidWestSleepAppDelegate

@synthesize窗口; @synthesize viewController; @synthesize得分;

- (void)applicationDidFinishLaunching:(UIApplication *)application{
LogMethod();
// If you want the status bar to be hidden at launch use this:
// application.statusBarHidden = YES;
//
// To set the status bar as black, use the following:
// application.statusBarStyle = UIStatusBarStyleBlackOpaque;


// Create window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// this helps in debugging, so that you know "exactly" where your views are placed;
// if you see "red", you are looking at the bare window, otherwise use black
// window.backgroundColor = [UIColor redColor];

viewController = [ [ MainViewController alloc ] init ];

navigationController = [ [ UINavigationController alloc ] initWithRootViewController: viewController ];

/* Anchor the view to the window */
[window addSubview:[navigationController view]];

/* Make the window key and visible */
[window makeKeyAndVisible];}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation{
return YES;} 

@end

在上面的代码中,当我实现时,我在模拟器中遇到问题,在横向模式下整个uiview变得不同而没有得到自己的位置。这段代码有什么问题,如何解决?

1 个答案:

答案 0 :(得分:0)

-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation myview=self.interfaceOrientation;
    [UIView beginAnimations:@"move Button" context:nil];
    if (myview==UIInterfaceOrientationPortrait || myview==UIInterfaceOrientationPortraitUpsideDown) 
    {   //here is the frames for portrait orientation
        coverimage.frame=CGRectMake(2, 2, 315, 310);

        myscrollview.frame=CGRectMake(2, 305,320, 480);

    }
    else
    {   
                //here is the frames for landscape orientation
        coverimage.frame=CGRectMake(65,10,330,180);
        myscrollview.backgroundColor=[UIColor clearColor];
        myscrollview.frame=CGRectMake(2, 150,500,250);
    }
    [UIView commitAnimations];

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