如何打开/关闭自动布局在故事板的单一视图控制器 [英] How to turn on/off auto layout for a single view controller in storyboard

查看:132
本文介绍了如何打开/关闭自动布局在故事板的单一视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开启/关闭自动布局在故事板

I want to turn on/off auto layout for a single view controller in storyboard (called scene I believe)

当我去到故事板特定场景,并更改使用自动布局复选框中的检查,我得到了整个故事板的场景开启/关闭自动布局。

When I go to a specific scene in storyboard, and change the Use Auto Layout checkbox in the inspector, I get the auto layout on/off for the entire storyboard scenes.

我不明白这种行为,因为一此复选框看起来像一个特定场景的属性,而不是整个故事板,二是我不明白为什么一个场景布局有事情做与其他场景。

I can't understand this behaviour, because for one this checkbox looks like a property of a specific scene and not the entire storyboard, and second I don't see why one scene layout has something to do with the other scenes.

这甚至可能吗?是否有可能在故事板或者只在code?

Is this even possible? Is it possible in storyboard or maybe only in code?

推荐答案

这可能看起来像这种使用自动布局复选框是针对特定场景,但正如你所发现的,它是整个Interface Builder的文档 (即,整个故事板)。因此,如果你不希望在一个特定的场景使用自动布局,你被卡住了几个备选方案:

It may look like this "Use Auto Layout" checkbox is for a specific scene, but as you've discovered, it's for the entire "Interface Builder Document" (i.e., the entire storyboard). Thus, if you don't want to use auto layout on a particular scene, you are stuck with a few alternatives:


  1. 您可以把这个场景等以不同的故事板或笔尖和过渡,并从视图控制器编程。如果你这样做(例如,你不能只是建立在塞格斯IB来回,而是要过渡到从这个场景编程),你失去很多的故事板的好处。

  1. You can either put this other scene in a different storyboard or NIB and transition to and from view controller programmatically. You lose many of the storyboard benefits if you do that (e.g. you cannot just create segues back and forth in IB, but rather have to transition to and from this scene programmatically).

您可以继续自动布局启用,但随后有问题的一个视图控制器,可以通过编程:

You could keep auto layout enabled but then for the one view controller in question, you can programmatically:


  • 删除IB可能已经添加(尤指X code 6)任何约束;

  • Remove any constraints that IB may have added (esp in Xcode 6);

调整 autoresizingMask 为您的各种控制。如果你希望你的 autoresizingMask 来兑现,你可能想打开 translatesAutoresizingMaskIntoConstraints ;以及

Adjust the autoresizingMask for your various controls. If you want your autoresizingMask to be honored, you may want to turn on translatesAutoresizingMaskIntoConstraints; and

如果使用X code 6,您可能要关闭使用大小类,也让你在目标设备正确地布置现场。

If using Xcode 6, you might want to turn off "Use Size Classes", too, so that you're laying out the scene properly for the target device.

所以,已经奠定了我的标签,我在IB想要的方式,然后我可以:

So, having laid out my label the way I wanted in IB, I could then:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view removeConstraints:self.view.constraints];
    [self.label removeConstraints:self.label.constraints];
    self.label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    self.label.translatesAutoresizingMaskIntoConstraints = YES;
}

显然,你将 autoresizingMask 来完全取决于你(在这个例子中,我把集中在现场的标签,因此,上述保留它为中心,因为我去横向),但希望这说明了想法。

Clearly, what you set the autoresizingMask to is entirely up to you (in this example, I put a label centered on scene, so the above keeps it centered as I go to landscape), but hopefully this illustrates the idea.

坦率地说,因为没有什么可以与你不能轻易的前$汽车布局p $ PSS,我倾向于坚持使用自动布局自动调整大小面具做的,你消除上述方法的尴尬

Frankly, given that there's nothing you can do with autosizing masks that you can't easily express in auto layout, I'd be inclined to stick with auto layout, and you eliminate the awkwardness of the above approaches.

这篇关于如何打开/关闭自动布局在故事板的单一视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆