0
votes

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. 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. The constraints I need to convert from storyboard to code are

BIG CONTAINER 375x667:

  • Trailing space to superView (or something to have its right side adapt to every device, I guess superview should work fine);
  • Width and height equal to view;
  • Leading space to small container view (0).

SMALL CONTAINER 240x667

  • Width equals to 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.

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)
1

1 Answers

0
votes

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)

])