请用故事板自动版式中心UIButtons [英] Keep UIButtons centered using autolayout in storyboard

查看:166
本文介绍了请用故事板自动版式中心UIButtons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含视图中两个按钮(以precise,一个的UITableView 页脚):

I have two buttons contained within a view (to be precise, a UITableView footer):

我想保持居中的按钮(与在中间的间隙),而不管在上海华的宽度(这将增加,例如,如果设备被旋转为纵向)的

I would like to keep the buttons centered (with the gap in-between) regardless of the width of the superview (which will increase, for example, if the device is rotated to portrait orientation).

有没有办法在故事板纯粹定义这些自动布局的限制,否则我将需要使用 NSLayoutConstraints 在code?

Is there a way to define these autolayout constraints purely in the storyboard, or will I need to use NSLayoutConstraints in code?

推荐答案

可以实现通过添加约束为每个按钮对准前导/尾随到容器视图的中心,加上它们之间的一些分离。

You can achieve that by adding a constraint for each button to align the leading/trailing to the center of the container view, plus some separation between them.

如何做到这一点:


  • 有关左侧的按钮,将按钮添加和上海华之间的约束水平居中的。

  • For the left side button, add a constraint between the button and superview to center it horizontally.

编辑约束和改变第二项属性是button1.trailing,并设置为10(或任何距离你想要的按钮右侧边框和屏幕中心之间)的常数。

Edit the constraint and change the "Second Item" attribute to be button1.trailing, and set a constant of 10 (or whatever distance you want between the right border of the button and the center of the screen).


  • 有关右侧的按钮,添加按钮和上海华之间的约束水平居中的。

  • For the right side button, add a constraint between the button and superview to center it horizontally.

编辑约束和改变第二项属性是button2.leading并设置-10(或任何距离你想要的按钮的左边框与屏幕中心之间)的常数。

Edit the constraint and change the "Second Item" attribute to be button2.leading and set a constant of -10 (or whatever distance you want between the left border of the button and the center of the screen).

这篇关于请用故事板自动版式中心UIButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆