简体   繁体   English

如何为故事板中的单个视图控制器打开/关闭自动布局

[英]How to turn on/off auto layout for a single view controller in storyboard

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. 当我转到故事板中的特定场景,并更改检查器中的“ Use Auto Layout复选框时,我会为整个故事板场景打开/关闭自动布局。

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. 我无法理解这种行为,因为对于一个这个复选框看起来像一个特定场景的属性而不是整个故事板,其次我不明白为什么一个场景布局与其他场景有关。

Is this even possible? 这甚至可能吗? Is it possible in storyboard or maybe only in code? 它可以在故事板中,也可能只在代码中?

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" (ie, 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. You can either put this other scene in a different storyboard or NIB and transition to and from view controller programmatically. 您可以将此其他场景放在不同的故事板或NIB中,并以编程方式转换到视图控制器和从视图控制器转换。 You lose many of the storyboard benefits if you do that (eg you cannot just create segues back and forth in IB, but rather have to transition to and from this scene programmatically). 如果你这样做,你将失去许多故事板的好处(例如,你不能只在IB中来回创建segues,而是必须以编程方式过渡到这个场景)。

  2. You could keep auto layout enabled but then for the one view controller in question, you can programmatically: 您可以启用自动布局,但是对于有问题的一个视图控制器,您可以以编程方式:

    • Remove any constraints that IB may have added (esp in Xcode 6); 删除IB可能添加的任何约束(特别是在Xcode 6中);

    • Adjust the autoresizingMask for your various controls. 调整autoresizingMask以进行各种控制。 If you want your autoresizingMask to be honored, you may want to turn on translatesAutoresizingMaskIntoConstraints ; 如果您想要兑现autoresizingMask ,您可能需要打开translatesAutoresizingMaskIntoConstraints ; and

    • 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. 如果使用Xcode 6,您可能还想关闭“使用大小类”,以便正确地为目标设备布置场景。

    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; } 

    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. 显然,你设置autoresizingMask的方法完全取决于你(在这个例子中,我把一个标签放在场景中心,所以上面让它保持居中,因为我去景观),但希望这说明了这个想法。

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. 坦率地说,鉴于你在自动布局中不能轻易表达的自动调整掩模,你无法做任何事情,我倾向于坚持自动布局,并且你消除了上述方法的尴尬。

You can move your single view controller's layout to a separate xib file and load it programmatically. 您可以将单个视图控制器的布局移动到单独的xib文件并以编程方式加载它。 And if you simply don't want to define your own constraints, you can leave your controller in the storyboard and simply not add constraints to it. 如果您只是不想定义自己的约束,可以将控制器留在故事板中,而不是添加约束。 In that case, constraints will be automatically generated for you at runtime, based on the VC's child views' frames. 在这种情况下,将根据VC的子视图框架在运行时自动为您生成约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM