Search code examples
iosautolayoutcenter

iOS Set autoLayout button place to the remain space at center in storyboard


I have a autolayout problem.

I use storyboard to drag the constraints.

I have a top green view. It was fix height. And the green view left,top and right constraints are 0.

The bottom view is blue. It was fix height. And the blue view left, bottom and the right constraints are 0.(The height is different of the green view)

Then I put a yellow test button. I need put the center remain the space of the white.

How can I set the constraints use the storyboard in the remain the white space.

enter image description here

I had try to drag the test button vertical space constraint to the BlueView.Top. Then set the Multiplier to 0.5.

But it was not feasible.

Have anyone can tutorial how to set and drag the constraint about this situation?

Thank you very much.

I wish the test button in the remain space center like this photo. enter image description here


Solution

  • It's not difficult. Just put an empty view between the green and blue area, set necessary constraints so that this empty view fills the white area fully, and then put the button into the middle of that empty view. Set constraints on the button to keep it in the middle.

    And don't be inconvenienced by having a not that useful view in your layout. Since it is transparent, the rendering system will deal with it accordingly, you don't even need to reference it in code. It really is just a dumb view helping you achieve the expected layout.