【发布时间】:2016-08-01 21:21:29
【问题描述】:
【问题讨论】:
-
也许这个可以帮助你...stackoverflow.com/questions/32862142/…或者你可以使用stackview
标签: ios swift xcode autolayout constraints
【问题讨论】:
标签: ios swift xcode autolayout constraints
如果我是你,我会以编程方式制作它们。如果它们包含在视图中,这将起作用。下面的代码假设您希望在 viewController 中将按钮居中。
以编程方式制作 UIButtons
在 ViewDidLoad 中:
let myFirstXCoordinate = CGFloat((self.view.width / 4) - (myWidth / 2))
let mySecondXCoordinate = CGFloat((3 * self.view.width) / 4) - (myWidth / 2))
let myWidth:CGFloat = //your button's width
let myHeight:CGFloat = //your button's height
let myYCoordinate:CGFloat = //your Y Coordinate
firstButton.setBackgroundImage(UIImage(named: "myRedOvalThingyImage"), forState: .Normal)
firstButton.backgroundColor = UIColor.clearColor()
firstButton.frame = CGRectMake(myFirstXCoordinate, myYCoordinate, myWidth, myHeight)
firstButton.addTarget(self, action: #selector(ViewController.pressedFirst(_:)), forControlEvents: .TouchUpInside)
buttonView.addSubview(firstButton)
secondButton.setBackgroundImage(UIImage(named: "myRedOvalThingyImage"), forState: .Normal)
secondButton.backgroundColor = UIColor.clearColor()
secondButton.frame = CGRectMake(mySecondXCoordinate, myYCoordinate, myWidth, myHeight)
secondButton.addTarget(self, action: #selector(ViewController.pressedSecond(_:)), forControlEvents: .TouchUpInside)
buttonView.addSubview(secondButton)
ViewDidLoad 之外:
func pressedFirst(sender: UIButton!) {
print("First Oval Thingy Was Pressed")
}
func pressedSecond(sender: UIButton!) {
print("Second Oval Thingy Was Pressed")
}
【讨论】:
当你处理约束时,你必须工作块。
在这种情况下,您应该有 2 个块,它们的大小都与从 0 到中心的大小相同,并且以父级的宽度为中心。
示例:
Parent Width: 320
let widthBlock = 320/2
block 1: x = 0, width = widthBlock
block 2: x = widthBlock, width = widthBlock
然后在每个块中创建按钮并使用约束在垂直和水平方向居中。
因为你正在做的界面要简单得多,只需从按钮拖动到阻止并给他的父亲Center Horizontal in Container和Center vertically in container。
【讨论】: