Как динамически изменить ориентацию основных окон и других видов навигации по окну? - PullRequest
0 голосов
/ 12 июля 2011

У меня есть код, в котором все элементы пользовательского интерфейса создаются динамически. У меня есть контроллер навигации, который подключается к другому представлению в главном окне. В этом у меня проблема в том, что когда я пишу код для изменения ориентации, он не работает автоматически.

Прагма Марк MainViewController

@ реализация 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) Сайт: (идентификатор) отправитель { [[UIApplication sharedApplication] openURL: [NSURL URLWithString: @ "http://www.midwestsleepmed.com"]];}

Прагма Марк AppDelegate

Прагма Марк -

@ реализация MidWestSleepAppDelegate

@ синтезировать окно; @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;} 

@ конец

В приведенном выше коде, когда я реализую, я получаю проблему в симуляторе, который находится в ландшафтном режиме, весь пользовательский интерфейс становится другим и не получает свою собственную позицию. Что это за проблема в этом коде и как мне это исправить?

1 Ответ

0 голосов
/ 12 июля 2011
-(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);
}
...