iPhone的自动取向

发布于 2024-12-04 05:38:10 字数 195 浏览 0 评论 0原文

我使用 UIDeviceOrientationDidChangeNotification 来获取横向和肖像模式的通知。在我的项目中,我需要相应地更改放置在 UIView 上的 UIControls 的位置。我是否必须手动更改每个 UIControl 的帧大小,因为这很忙?

I have used UIDeviceOrientationDidChangeNotification to get notified of landscape and potrait mode. In my project I'm required to change the positions of UIControls placed on the UIView accordingly. Do I have to change the frame size manually for each UIControl, as this is hectic?

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

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

发布评论

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

评论(2

如果没结果 2024-12-11 05:38:10

在您的视图控制器中,您应该实现方法:

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

对于支持的方向,此方法应该返回YES

如果您希望子视图自动调整大小和位置,那么您应该为该视图设置适当的属性autoresizingMask。例如:

blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
                                 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
                                 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

In your view controller you should implement method:

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

This method should return YES for supported orientations.

If you want your subviews to resize and reposition automatically then you should set appropriate property autoresizingMask of that views. For example:

blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
                                 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
                                 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
围归者 2024-12-11 05:38:10

您确实需要使用 shouldAutorotateToInterfaceOrientation... 方法,但在它正常工作之前您需要做更多的事情。首先,您需要两个 xib 文件,一个用于横向,一个用于纵向。然后,您需要像这样使用您的函数:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
    //prepare for a Landscape view
    }
else{
    //prepare for a portrait view
    }
    return YES;
}

至于准备横向或纵向视图,使用自动调整大小可以工作,但您可能需要完全独立的 xib。我找不到更改活动视图控制器内的 xib 的方法,因此我建议创建一个单独的视图控制器实例。示例:

当您展示 viewController 的第一个实例时,将其与肖像 xib 一起展示。您可能还需要一个布尔值来跟踪当前的 xib。然后实现此代码:

/*Assuming you are using a viewcontroller called viewController, and you have a boolean called xibPortrait.
You have presented this view controller using the portrait xib.
You also have a landscape xib, Landscape.xib
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) && xibPortrait){
        xibPortrait = FALSE;
        viewController *Land = [[ViewController alloc] initWithNibName:@"Landscape" bundle:nil];
        [self presentModalViewController:Land animated:NO];
        [Land release];
    }
else if(!xibPortrait){
    [self.parentViewController dismissModalViewControllerAnimated:NO];
    xibPortrait = TRUE;
    }
return YES;
}


//This should work but if you can, you should try to use Autoresizing.

编辑:
我还发现有人在我的一个问题上发布了这一点。与使用我上面的方法相比,它可能会帮助您的应用程序更有效地工作:
http://aseriesoftubes.com/articles/ipad-development-101-orientation/

You do need to use the shouldAutorotateToInterfaceOrientation... method but you need to do a little more before it will work properly. First of all, you will need two xib files, one for a landscape orientation and one for a portrait orientation. You will then need to use your function like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
    //prepare for a Landscape view
    }
else{
    //prepare for a portrait view
    }
    return YES;
}

As for preparing for landscape or portrait views, using autoresizing can work but you may need to have totally seperate xibs. I could not find a way to change the xib within the active view controller, so I would suggest making a seperate instance of your view controller. Example:

When you present your first instance of the viewController, present it with the portrait xib. You may also need a boolean to keep track of the current xib. Then have this code implemented:

/*Assuming you are using a viewcontroller called viewController, and you have a boolean called xibPortrait.
You have presented this view controller using the portrait xib.
You also have a landscape xib, Landscape.xib
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) && xibPortrait){
        xibPortrait = FALSE;
        viewController *Land = [[ViewController alloc] initWithNibName:@"Landscape" bundle:nil];
        [self presentModalViewController:Land animated:NO];
        [Land release];
    }
else if(!xibPortrait){
    [self.parentViewController dismissModalViewControllerAnimated:NO];
    xibPortrait = TRUE;
    }
return YES;
}


//This should work but if you can, you should try to use Autoresizing.

EDIT:
I also found this which someone posted on one of my questions. It may help your app work more efficiently than using my method above:
http://aseriesoftubes.com/articles/ipad-development-101-orientation/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文