Q
如何居中两个标签
0
A
回答
1
如果我是你,我会让他们编程。如果它们包含在视图中,这将起作用。下面的代码假设你想将按钮置于你的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")
}
0
当您使用约束时,您必须工作。
在这种情况下,您应该有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
。
0
0
相关问题
- 1. 居中标签
- 2. 如何从两个标签
- 3. 如何两个标签
- 4. 在UserControl中居中标签
- 5. Xamarin.Forms:如何在一个行中居中多标签?
- 6. 居中标签+下拉一个div
- 7. 使用CSS居中标签
- 8. 如何交换两个div标签?
- 9. 如何添加空两个div标签
- 10. 如何合并两个XML标签
- 11. 在img标签中居中标注<a>父标签
- 12. 如何基于两个不同的标签合并xml标签?
- 13. 取两个标签
- 14. 如何在六个标签中隐藏两个随机标签的文字?
- 15. 如何在一个div中垂直居中放置一个标签?
- 16. 如何在Android中的标签栏中居中对齐文本
- 17. 两个子标签顶部的标签
- 18. 中心徽标两个标签
- 19. 在TableView中居中标签和UITextField
- 20. 标签中的垂直居中文本
- 21. 在Wordpress中居中跨度标签?
- 22. CollectionViewCell中的标签/图像不居中
- 23. 在UINavigationBar中垂直居中标签TitleView
- 24. 居中DIV中的锚标签
- 25. 如何将锚标签中的文本居中?
- 26. 如何在WPF中居中标签文本?
- 27. 如何在swift中编程式地将verticaly标签居中?
- 28. 如何使用网格将tkinter colspan中的标签居中?
- 29. 如何在div标签中居中(垂直和水平)按钮?
- 30. 如何在C3.js饼图中居中放置标签
可能是这个帮助你... http://stackoverflow.com/questions/32862142/how-to-add-equal-spacing-and-equal-width-for-button-in-ios-auto-布局/ 32862693#32862693或者您可以使用堆栈视图 –