xcode iPhone Storyboard:人像和风景的不同场景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8777629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:12:51  来源:igfitidea点击:

iPhone Storyboard: different scene for portrait and landscape

iphonexcodeinterface-builderstoryboardscreen-orientation

提问by Korbi

If you scroll down a bit at this Apple Developer Pageyou'll find the section "Creating an Alternate Landscape Interface". The basic approach described there is to present a different NIB file as a modal view when orientation changes. I am using the Storyboard feature, so I don't have NIBs. How do I load a different "scene" in that case?

如果您在此Apple Developer Page向下滚动一点,您将找到“创建备用横向界面”部分。那里描述的基本方法是在方向更改时将不同的 NIB 文件呈现为模态视图。我正在使用 Storyboard 功能,所以我没有 NIB。在这种情况下如何加载不同的“场景”?

Besides that, I am using a Tab Bar controller, I don't want to show a modal view. I just want to replace the current portrait view with a landscape view designed with interface builder and keep my tab bar. What would be the Storyboard way to achieve the task "Creating an Alternate Landscape Interface"? Thank you.

除此之外,我使用的是 Tab Bar 控制器,我不想显示模态视图。我只想用界面构建器设计的横向视图替换当前的纵向视图并保留我的标签栏。实现“创建替代横向界面”任务的故事板方式是什么?谢谢你。

采纳答案by T.J.

When you add a view controller to the storyboard it comes with a view. Call that the container view. Add two views to the container view: a portrait view and a landscape view. Set the dimension of the portrait view and the landscape view appropriately using the size inspector. Add buttons, more views, labels or whatever to the portrait and landscape views as needed for your application. Then when the orientation changes hide one view and show the other.

当您将视图控制器添加到情节提要时,它会附带一个视图。称之为容器视图。向容器视图添加两个视图:纵向视图和横向视图。使用尺寸检查器适当地设置纵向视图和横向视图的尺寸。根据您的应用程序的需要,向纵向和横向视图添加按钮、更多视图、标签或任何内容。然后当方向改变时隐藏一个视图并显示另一个。

回答by ttosh

You can setup a navigation controller and one main view. Then you can use a template view for the portrait and landscape layouts (2 additional views).

您可以设置一个导航控制器和一个主视图。然后您可以使用纵向和横向布局的模板视图(2 个附加视图)。

You will need to setup the controls on the main view and make sure each one has a unique tag. Your main view will not be used, instead you will copy the controls to the two template views and set them up based on how you want each view to look. The benefit to this is each view will retain its tag which becomes a very important piece of this implementation.

您需要在主视图上设置控件并确保每个控件都有唯一的标签。将不会使用您的主视图,而是将控件复制到两个模板视图中,并根据您希望每个视图的外观进行设置。这样做的好处是每个视图都将保留其标记,这将成为此实现的一个非常重要的部分。

Doing this you use a hybrid approach in regards to writing some UI code and using Interface Builder. After getting the two templates setup, create a unique identifier for each one. You will have to write some logic to handle the view and its subviews. A recursive method to return a collection of these based on the template you choose.

为此,您可以使用混合方法编写一些 UI 代码和使用 Interface Builder。获得两个模板设置后,为每个模板创建一个唯一标识符。您将不得不编写一些逻辑来处理视图及其子视图。根据您选择的模板返回这些集合的递归方法。

The core logic in the root view controller implementation will need to check for isPortrait and based on this you will want to load the correct view based on the identifier.

根视图控制器实现中的核心逻辑将需要检查 isPortrait 并且基于此,您将希望根据标识符加载正确的视图。

Experiment with this concept and see if it works for you. The main benefits to not using two separate views with unique controls (not the shared approach with same tags) is that you maintain access to your original subviews. Any instance variables you define in your view controller that points to a text filed, label, etc... continue to do so regardless of which template view is used. This maintains the model, view, controller approach as the data structure remains unchanged.

试验这个概念,看看它是否适合你。不使用具有独特控件的两个单独视图(不是具有相同标签的共享方法)的主要好处是您可以保持对原始子视图的访问。您在视图控制器中定义的指向文本字段、标签等的任何实例变量都会继续这样做,无论使用哪个模板视图。这保持了模型、视图、控制器方法,因为数据结构保持不变。

Using this approach you can still maximize the use of interface builder, and layout the templates for each view, while still having the flexibility to to write some custom UI code if you desire. Using only interface builder can be a bit limiting at times, and writing custom code based on orientation locks you in to a bit of tedious work.

使用这种方法,您仍然可以最大限度地使用界面构建器,并为每个视图布局模板,同时仍然可以根据需要灵活地编写一些自定义 UI 代码。仅使用界面构建器有时会受到一些限制,并且基于方向编写自定义代码会使您陷入一些乏味的工作中。

Hope this helps some.

希望这对一些人有所帮助。

回答by mattiad

You can make a xib file that contains 2 uiviews, one fore portrait and one for landscape. Assign as file's owner of the xib, the same viewcontroller of the view thet you have in the storyboard. In viewDidLoad load the xib file, and add the appropriete view for portrait or landscape.

您可以制作一个包含 2 个 uiviews 的 xib 文件,一个前肖像和一个风景。将您在故事板中拥有的视图的相同视图控制器指定为 xib 的文件所有者。在 viewDidLoad 中加载 xib 文件,并为纵向或横向添加适当的视图。

So if you have a storyboard with many viewcontrollers, you can set the two possibility (portrait or landscape view) only in the viewcontrollers that you are interested to change the orientation.

因此,如果您有一个带有许多视图控制器的故事板,您可以仅在您有兴趣更改方向的视图控制器中设置两种可能性(纵向或横向视图)。

I used this solution and work very fine !

我使用了这个解决方案并且工作得很好!