简体   繁体   English

如何以编程方式为容器视图设置自适应约束

[英]How to set adaptable constraints programmatically for container views

NB: Right now I both have programmatically set constraints and storyboard constraints. 注意:现在我都以编程方式设置约束和故事板约束。

  1. Question one: 问题一:

If I keep both programmatically and storyboard constraints, I get errors in my storyboard but the app works fine ( I replaced some storyboard constraints with the programmatical ones). 如果我保持编程和故事板约束,我在我的故事板中得到错误但应用程序工作正常(我用一些程序设计替换了一些故事板约束)。 But can I do that? 但我能这样做吗? Would apple accept that? 苹果会接受吗?

If not 如果不

  1. Question two: 问题二:

I have a ViewController where I put two container views, each with different dimensions in order to create a sort of side menu. 我有一个ViewController,我放置了两个容器视图,每个视图都有不同的尺寸,以创建一种侧面菜单。 I have a big one as big as the screen and a small one that is as long as the screen but wide half the screen. 我有一个像屏幕一样大的大屏幕和一个与屏幕一样长但屏幕宽一半的小屏幕。 At first I added my constraints in storyboard, but now I realised I need to set them programmatically to achieve what I need. 起初我在storyboard中添加了我的约束,但现在我意识到我需要以编程方式设置它们以实现我需要的东西。 The constraints I need to convert from storyboard to code are 我需要从故事板转换为代码的约束

BIG CONTAINER 375x667: 大容器375x667:

  • Trailing space to superView (or something to have its right side adapt to every device, I guess superview should work fine); 尾随空间到superView(或者其右侧适应每个设备的东西,我想superview应该可以正常工作);
  • Width and height equal to view; 宽度和高度等于视图;
  • Leading space to small container view (0). 前导空间到小容器视图(0)。

SMALL CONTAINER 240x667 小型集装箱240x667

  • Width equals to 240; 宽度等于240;
  • Height equals the big container's one; 高度等于大容器的高度;

  • Trailing space to the big container (there's no need to add this again here if we already set the big container's leading space); 跟踪大容器的空间(如果我们已经设置了大容器的前导空间,则无需再次添加此容器);

  • Align leading to safe area equals -240. 对齐导致安全区域等于-240。

Here's what I have already done (correct part): 这是我已经完成的(正确的部分):

func containerViewsConstraints() {
 containerView1.translatesAutoresizingMaskIntoConstraints = false

        containerView1.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        containerView1.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true


        containerView2.translatesAutoresizingMaskIntoConstraints = false
        containerView2.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        containerView2.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

Here's what I tried (Not sure if correct): 这是我尝试过的(不确定是否正确):

containerView2.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        containerView2.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

    containerView2.widthAnchor.isEqual(view.widthAnchor)
        containerView2.heightAnchor.isEqual(view.heightAnchor)

        containerView1.widthAnchor.constraint(equalToConstant: 240)
        containerView1.heightAnchor.isEqual(view.heightAnchor)

You need 你需要

bigView.backgroundColor = .red
smallView.backgroundColor = .green 
bigView.translatesAutoresizingMaskIntoConstraints = false
smallView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bigView)
view.addSubview(smallView)
NSLayoutConstraint.activate([

    bigView.topAnchor.constraint(equalTo: view.topAnchor),
    bigView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    bigView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    bigView.trailingAnchor.constraint(equalTo: view.trailingAnchor),

    smallView.topAnchor.constraint(equalTo: view.topAnchor),
    smallView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    smallView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    smallView.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier:0.5)

])

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

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