开发者

How to change orientation of main windows and other views navigated by window dynamically?

开发者 https://www.devze.com 2023-03-19 08:12 出处:网络
I have a code in which all UI items created by dynamically. I have a navigation controller which connect to other view to main window. In this I have a problem that when I write a code for orientation

I have a code in which all UI items created by dynamically. I have a navigation controller which connect to other view to main window. In this I have a problem that when I write a code for orientation change then it not work automatically.

pragma mark 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:@"midw开发者_StackOverflow中文版estsleep.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)Site:(id)sender{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.midwestsleepmed.com"]];}

pragma mark AppDelegate

pragma mark -

@implementation MidWestSleepAppDelegate

@synthesize window; @synthesize viewController; @synthesize score;

- (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

In above code when i implement then i get problem in simulator that is in landscape mode whole uiview becomes different and not getting their own position. What it is problem in this code and how do I get it fixed?


-(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);
}
0

精彩评论

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

关注公众号