在 iPhone 中保持应用程序的布局处于纵向和横向模式

发布于 2024-10-21 03:53:47 字数 545 浏览 2 评论 0原文

我的应用程序中有一个视图,它具有三个分段按钮控制器,并且我用纵向模式设计了它。当我旋转屏幕时,整个用户界面......就是不正确。

在此处输入图像描述 在此处输入图像描述

我的问题是,我应该分别针对纵向和横向模式进行设计吗?如果是这样我该怎么做?

附注实际上,我不需要 iPhone 应用程序的旋转支持,但我计划为 iPad 制作相同的应用程序,其中每个视图都必须可旋转。谢谢。


编辑: 刚刚找到了很好的例子 支持多个方向的最简单方法?当应用程序处于横向模式时,如何加载自定义 NIB?

I have a view in my app that has three segmentedButtonControllers and I designed it with portrait mode. When I rotate the screen the whole UI is...just not right.

enter image description here
enter image description here

My question is, should I design for both portrait and landscape mode separately? If so how I do that?

ps. Actually I don't need rotation support for iPhone's app but I'm planning to make same app for iPad where every view has to be rotatable.thanks.


Edit:
just found good example
Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape?

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

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

发布评论

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

评论(4

追我者格杀勿论 2024-10-28 03:53:47

我想说这取决于内容。有时,在 Interface Builder 的检查器中正确设置 UI 元素的自动调整大小就足够了,并且您可以获得令人满意的效果,但在某些情况下,仅调整视图大小并不能充分利用屏幕空间。例如,在您的情况下,仅将分段控件扩展到整个宽度可能不如将它们排列在一起以在底部放置文本来得好。

如果您要分别设计这两种模式,则可以将它们作为不同的视图并在相应的视图之间进行切换。很好的例子可以在 iPhone Cookbook 的第 4 章中找到。作者所做的是当方向改变时切换视图,如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
 { 
     if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
       self.view = landscapeView;
    else if ((interfaceOrientation == UIInterfaceOrientationPortrait)
        || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
       self.view = portraitView;
    return YES;
 }

I'd say it depends on the content. Sometimes it's enough to set the autoresizing of the UI elements in the Inspector of the Interface Builder properly and you get a satisfying effect, but there are cases where just resizing the view doesn't make the best use of the screen real estate. In your case for example just extending the segmented controls to the full width is probably not going to be as nice as arranging them next to each other to make the place for the text at the bottom.

If you are going to design for both modes separately, you can have them as different views and switch between the accordingly. Good example can be found in the 4th chapter of iPhone Cookbook. What the author does there is to switch the view when the orientation changes as follows :

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
 { 
     if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
       self.view = landscapeView;
    else if ((interfaceOrientation == UIInterfaceOrientationPortrait)
        || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
       self.view = portraitView;
    return YES;
 }
薄情伤 2024-10-28 03:53:47

我强烈建议您在文档中查找 UIViewController 类并阅读全部内容(假设您还没有)。您需要的关键方法包括 willRotateToInterfaceOrientation:duration:,描述如下:

willRotateToInterfaceOrientation:duration:
在用户界面开始旋转之前发送到视图控制器。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration

参数toInterfaceOrientation
用户的新方向
界面。可能的值为
UIInterfaceOrientation 中描述。
duration 待处理的持续时间
旋转,以秒为单位。
讨论子类可能会覆盖
此方法执行额外的
紧接之前的行动
旋转。例如,您可能会使用
此方法可禁用视图
交互、停止媒体播放或
暂时关闭昂贵的绘图
或实时更新。你也可能会使用它
将当前视图交换为
反映新界面
方向。当这个方法是
称为,接口方向
属性仍然包含视图的
原来的方向。

无论什么情况都会调用此方法
您的代码是否执行单步执行或
两步旋转。

适用于 iOS 2.0 和
稍后。

听起来您还应该包括:

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

I strongly recommend you look up the UIViewController class in docs and read it all (assuming you haven't already). The key methods you need include willRotateToInterfaceOrientation:duration:, described as follows:

willRotateToInterfaceOrientation:duration:
Sent to the view controller just before the user interface begins rotating.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

Parameters toInterfaceOrientation The
new orientation for the user
interface. The possible values are
described in UIInterfaceOrientation.
duration The duration of the pending
rotation, measured in seconds.
Discussion Subclasses may override
this method to perform additional
actions immediately prior to the
rotation. For example, you might use
this method to disable view
interactions, stop media playback, or
temporarily turn off expensive drawing
or live updates. You might also use it
to swap the current view for one that
reflects the new interface
orientation. When this method is
called, the interfaceOrientation
property still contains the view’s
original orientation.

This method is called regardless of
whether your code performs one-step or
two-step rotations.

Availability Available in iOS 2.0 and
later.

It sounds like you should also include:

 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
栀子花开つ 2024-10-28 03:53:47

只需将控件的 autoresizingMask 属性设置为

UIViewAutoresizingFlexibleWidth

Just set the autoresizingMask property of your control to

UIViewAutoresizingFlexibleWidth
弱骨蛰伏 2024-10-28 03:53:47

我一直在寻找解决方案,最后选择以编程方式设置布局元素,如 这篇文章

I was looking for a solution and finally have chosen to set the layout elements programatically, as described in this article.

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