Ios 使用自动布局将图像和文本设置为按钮
我试图创建一个按钮,在文本右侧有一个下拉箭头,程序如下:Ios 使用自动布局将图像和文本设置为按钮,ios,swift,uibutton,Ios,Swift,Uibutton,我试图创建一个按钮,在文本右侧有一个下拉箭头,程序如下: 我看到的解决方案使用了标题和图像插入,但是有没有一种方法可以通过编程的方式使用autoLayout来设置它们?根据所选的选项,按钮中的文本可能会更改,文本长度也会不同,因此我不确定标题和边缘插入是否合适。否,无法使用AutoLayout在UIButton上设置图像和标题布局属性 如果您希望在UIButton中为图像和标题创建完全自定义的布局,我建议您创建一个UIView,并使用AutoLayout将Title和Image添加为子视图,然
我看到的解决方案使用了标题和图像插入,但是有没有一种方法可以通过编程的方式使用autoLayout来设置它们?根据所选的选项,按钮中的文本可能会更改,文本长度也会不同,因此我不确定标题和边缘插入是否合适。否,无法使用AutoLayout在UIButton上设置图像和标题布局属性 如果您希望在UIButton中为图像和标题创建完全自定义的布局,我建议您创建一个
UIView
,并使用AutoLayout将Title
和Image
添加为子视图,然后向UIView添加一个点击手势识别器
buttonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.buttonAction)))
不,无法使用AutoLayout在UIButton上设置图像和标题布局属性 如果您希望在UIButton中为图像和标题创建完全自定义的布局,我建议您创建一个
UIView
,并使用AutoLayout将Title
和Image
添加为子视图,然后向UIView添加一个点击手势识别器
buttonView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.buttonAction)))
这是一个将
UIStackView
放置在主VC容器视图中的示例(在我的例子中,UIStackView
占用了VC中的所有可用空间)。在这种情况下,基本用户信息会添加一个电话号码
我创建了一个电话号码容器视图(UIView
),一个包含电话号码的UILabel
,以及一个用于下拉箭头的UIImageView
let telNoContainerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let telNoLabel: UILabel = {
let view = UILabel()
let font = UIFont.systemFont(ofSize: 15)
view.font = font
view.backgroundColor = UIColor.white
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let telNoImageView: UIImageView = {
let view = UIImageView()
view.backgroundColor = UIColor.white
view.tintColor = ACTION_COLOR
view.image = UIImage(named: "Chevron")?.withRenderingMode(.alwaysTemplate)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
在setBasicInfoViews()
中,只需将telNoContainerView
添加到UIStackView
。然后将UILabel
和UIImageView
添加到包含视图telNoContainerView
。然后根据需要添加约束。
您需要更改约束以适合您的UI设计
fileprivate func setBasicInfoViews(){
infoStackView.addArrangedSubview(telNoContainerView)
telNoContainerView.addSubview(telNoLabel)
telNoContainerView.addSubview(telNoImageView)
NSLayoutConstraint.activate([
telNoLabel.topAnchor.constraint(equalTo: telNoContainerView.topAnchor, constant: 0.0),
telNoLabel.bottomAnchor.constraint(equalTo: telNoContainerView.bottomAnchor, constant: 0.0),
telNoLabel.leadingAnchor.constraint(equalTo: telNoContainerView.leadingAnchor, constant: 0.0),
telNoLabel.trailingAnchor.constraint(equalTo: telNoContainerView.trailingAnchor, constant: 0.0)
])
NSLayoutConstraint.activate([
telNoImageView.centerYAnchor.constraint(equalTo: telNoLabel.centerYAnchor, constant: 0.0),
telNoImageView.heightAnchor.constraint(equalToConstant: 30.0),
telNoImageView.widthAnchor.constraint(equalToConstant: 30.0),
telNoImageView.trailingAnchor.constraint(equalTo: telNoLabel.trailingAnchor, constant: 0.0)
])
}
这是一个将
UIStackView
放置在主VC容器视图中的示例(在我的例子中,UIStackView
占用了VC中的所有可用空间)。在这种情况下,基本用户信息会添加一个电话号码
我创建电话号码容器视图(UIView
),aui标签
以包含电话号码和下拉箭头的UIImageView
let telNoContainerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let telNoLabel: UILabel = {
let view = UILabel()
let font = UIFont.systemFont(ofSize: 15)
view.font = font
view.backgroundColor = UIColor.white
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let telNoImageView: UIImageView = {
let view = UIImageView()
view.backgroundColor = UIColor.white
view.tintColor = ACTION_COLOR
view.image = UIImage(named: "Chevron")?.withRenderingMode(.alwaysTemplate)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
在setBasicInfoViews()
中,只需将telNoContainerView
添加到UIStackView
。然后将UILabel
和UIImageView
添加到包含视图telNoContainerView
。然后根据需要添加约束。
您需要更改约束以适合您的UI设计
fileprivate func setBasicInfoViews(){
infoStackView.addArrangedSubview(telNoContainerView)
telNoContainerView.addSubview(telNoLabel)
telNoContainerView.addSubview(telNoImageView)
NSLayoutConstraint.activate([
telNoLabel.topAnchor.constraint(equalTo: telNoContainerView.topAnchor, constant: 0.0),
telNoLabel.bottomAnchor.constraint(equalTo: telNoContainerView.bottomAnchor, constant: 0.0),
telNoLabel.leadingAnchor.constraint(equalTo: telNoContainerView.leadingAnchor, constant: 0.0),
telNoLabel.trailingAnchor.constraint(equalTo: telNoContainerView.trailingAnchor, constant: 0.0)
])
NSLayoutConstraint.activate([
telNoImageView.centerYAnchor.constraint(equalTo: telNoLabel.centerYAnchor, constant: 0.0),
telNoImageView.heightAnchor.constraint(equalToConstant: 30.0),
telNoImageView.widthAnchor.constraint(equalToConstant: 30.0),
telNoImageView.trailingAnchor.constraint(equalTo: telNoLabel.trailingAnchor, constant: 0.0)
])
}
UIButton不以您可以访问的方式在内部使用自动布局。您可以创建类似视图的自定义按钮。UIButton不以您可以访问的方式在内部使用自动布局。您可以创建一个自定义按钮状视图。