如何在故事板中对齐两个uibutton

时间:2016-06-12 04:51:01

标签: swift button uibutton swift2

我如何制作两个按钮"喜欢"和"评论"在"左边的空间相等;和#34;,在"中间的空间相等;喜欢" "评论",以及"评论"右侧的相等空间取决于手机大小

这是一个图片示例:

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试以下代码,这是一个有点不同的方法,但希望它对你有用。最好直接在Storyboard中设置这些约束。

let deviceWidth = UIScreen.mainScreen().bounds.size.width
let buttonWidth = 75
let equalPadding = (deviceWidth - (2 * buttonWidth))/3
let centerXOfLikeButton = -(buttonWidth/2 + equalPadding/2)
let centerXOfCommentButton = (buttonWidth/2 + equalPadding/2)

// Like button constraints
let likeBtnTopConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)    
let likeBtnWidthConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem:  nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth)    
let likeBtnHeightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem:  nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 40)    
let likeBtnXConstraint = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute:  .CenterX, multiplier: 1, constant: centerXOfLikeButton)

self.view.addConstraint(likeBtnTopConstraint)
self.view.addConstraint(likeBtnWidthConstraint)
self.view.addConstraint(likeBtnHeightConstraint)
self.view.addConstraint(likeBtnXConstraint)

// Comment button constraints
let commentBtnTopConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)    
let commentBtnWidthConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem:  nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: buttonWidth)    
let commentBtnHeightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: .Equal,  toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 40)    
let commentBtnXConstraint = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view,  attribute: .CenterX, multiplier: 1, constant: centerXOfCommentButton)

self.view.addConstraint(commentBtnTopConstraint)
self.view.addConstraint(commentBtnWidthConstraint)
self.view.addConstraint(commentBtnHeightConstraint)
self.view.addConstraint(commentBtnXConstraint)

使用故事板,请参阅下面提供的屏幕截图 enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:0)

Crate两个宽度相等且高度相等的按钮,每个按钮上的“titleEdgeInsets”如下所示,

let insert = likeButton.frame.width / 4
    commentButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: insert)
    likeButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: insert, bottom: 0, right: 0)