position a view in storyboard at 1/3 of screen size

折月煮酒 提交于 2019-12-03 05:25:16

Create a constraint between the view's top edge and the superview's bottom edge. Set the constraint's constant to zero, and set its multiplier to 1:3. Like this:

You can setup it against center or bottom. Here is an example constraints related to center: (1/3 from top = 1.66 from center)

You can add empty (clear color) UIView, pin it to the top, and set it's height to be equal to 1/3 of the superview. Then you can pin the top of your yellow view to the bottom of your empty UIView.

For anyone trying to make this work programatically I was not able to handle that by 'anchors' style but old NSLayoutConstraint initializer worked:

let constraint = NSLayoutConstraint(item: floatingBtn,
                                    attribute: .centerY,
                                    relatedBy: .equal,
                                    toItem: view,
                                    attribute: .bottom,
                                    multiplier: 1/3,
                                    constant: 0)
constraint.isActive = true

Credits to accepted answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!