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

发布于 2024-11-19 19:45:17 字数 3325 浏览 2 评论 0原文

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

编译指示标记 MainViewController

@implementation MainViewController

@synthesize imageView; @synthesize btnBegin,btnSite; @合成电话号码;

 - (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"] ];}

编译指示标记 AppDelegate

编译指示标记 -

@implementation MidWestSleepAppDelegate

@synthesize window; @synthesize viewController; @合成分数;

- (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变得不同并且没有得到自己的位置。这段代码有什么问题以及如何解决它?

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:@"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)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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-11-26 19:45:17
-(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);
}
-(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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文