我UIButton
使用标准[button setTitle:@"Title"..]
我现在放置UILabel
在UIButton
的顶部,我不能设置标题,但我不能让标签在auto-layout
完美对齐。
注:我对UIButton
为的UIButton设置标题具有图像
回答
将图像设置为按钮的背景。然后标题应该是可见的。
如果您仍然需要在按钮上放置标签,则可以在代码中添加自动布局约束。他们在这种情况下工作。
编辑:斯威夫特例在操场使用:
import UIKit
import XCPlayground
let hostView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
hostView.layer.borderWidth = 1
hostView.layer.borderColor = UIColor.grayColor().CGColor
let button = UIButton.buttonWithType(.Custom) as! UIButton
button.backgroundColor = UIColor.redColor()
hostView.addSubview(button)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.setTitle("This is a really long title for this button", forState: .Normal)
button.titleLabel!.numberOfLines = 0
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-50-[button(100)]", options: nil, metrics: nil, views: ["button": button]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-50-[button(100)]", options: nil, metrics: nil, views: ["button": button]))
XCPShowView("Host View", hostView)
此works..but我需要在此更新软件包中选择图像。 。我的意思是,我可以增加标题行数,就像我可以在UILabel中设置的那样,使内容不会超出按钮的边界? –
将该按钮的textLabel的'numberOfLines'属性设置为0并使该按钮足够大。 – dasdom
无法让它工作..什么是确切的语法? –
您可以使用使用与标题和图片一个UIButton的图像,但你必须要照顾形象和标题edgeInset
例如[btn setTitleEdgeInsets:UIEdgeInsetsMake(70.0, -150.0, 5.0, 5.0)];
也看到这个answer为细节
在这种情况下,我会建议创建自己的自定义视图。 创建一个视图,并包括一个标签和一个UIImageView作为子视图
RootView:
++++++++++++
的UIImageView
------------- ---------
的UILabel
++++++++++++
那么你可以添加tapgesture到rootview模拟按钮的行为。
在这种情况下重新创建'UIButton'似乎是一种矫枉过正的行为。 – dasdom
设置按钮背景图像,并设置标题按钮
[button setBackgroundImage:[UIImage imageNamed:@"yourButtonImageName"] forState:UIControlStateNormal];
[button setTitle:@"Title" forState:UIControlStateNormal];
[button.titleLabel setNumberOfLines:0];
- 1. UIButton设置图像和标题
- 2. 我如何设置在UIButton图像前面的UIButton的标题
- 3. 设置UIButton的标题
- 4. 如何为UIButton设置标题?
- 5. 标题在UIButton与图像
- 6. 为UIButton设置一个背景图像
- 7. 在代码中为UIButton设置图像
- 8. 如何设置UIButton图像?
- 9. iOS - 界面构建器:设置图像时UIButton标题消失
- 10. UIButton的标题没有设置为空或iPhone中的空白
- 11. 当用标签处理UIButton图像时没有正确设置
- 12. 如何在Swift中为UIButton设置标题填充(插图)
- 13. 如何设置标题上的UIButton
- 14. 带标题和图像的UIButton
- 15. 标题和图像居中的UIButton
- 16. UIButton标题上方的中心图像
- 17. 为什么带有UIButton的UICollectionViewCell具有单色/有色图像?
- 18. 设置UIButton为
- 19. 如何将Android标题图标设置为下载图像
- 20. UIButton获取错误设置标题
- 21. Swift UIButton设置标题从Plist键
- 22. 清除uibutton标题并重新设置
- 23. 面具标题UIButton ios swift
- 24. ios7 - 如何将uibutton标题设置为上标?
- 25. 具有自定义图像的标准UIButton
- 26. 在UiButton中对齐图像和标题
- 27. 使用AFNetworking设置UIButton图像
- 28. 图像未在UIButton上快速设置
- 29. 用SVG文件设置UIButton图像
- 30. 设置UIButton的目标
通过从下拉选项卡上的右侧 –