您可以使用以下方法,
- 创建一个
UIView。
- 将
UILabel 添加到上面创建的view 作为subView。
- 将
UIButton 添加到上面创建的view 作为subView。
应用适当的layout constraints 以获得理想的用户界面。
func addSaveButton() {
let height: CGFloat = 60 + self.view.safeAreaInsets.bottom //Height based on safe area
//Custom View
let customView = UIView(frame: CGRect.init(x: 0, y: self.view.bounds.height - height, width: self.view.bounds.width, height: height))
customView.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
//Save Label
let label = UILabel()
label.text = "Save"
label.textColor = UIColor.black
//Button
let button = UIButton(frame: customView.bounds)
button.addTarget(self, action: #selector(onTapSaveButton), for: .touchUpInside)
//Add label, button as subview in customView
customView.addSubview(label)
customView.addSubview(button)
self.view.addSubview(customView)
customView.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
//Add constraints
NSLayoutConstraint.activate([
self.view.leadingAnchor.constraint(equalTo: customView.leadingAnchor),
customView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
customView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
customView.heightAnchor.constraint(equalToConstant: height),
label.topAnchor.constraint(equalTo: customView.topAnchor, constant: 10),
label.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -10),
button.topAnchor.constraint(equalTo: customView.topAnchor),
button.leadingAnchor.constraint(equalTo: customView.leadingAnchor),
button.trailingAnchor.constraint(equalTo: customView.trailingAnchor),
button.bottomAnchor.constraint(equalTo: customView.bottomAnchor)
])
}
@objc func onTapSaveButton() {
print("Save button pressed")
}
在 iPhone-X 中
在 iPhone-8 中
方法2:
您可以通过使用button's titleEdgeInsets来遵循更简化的方法。
func addSaveButton() {
let height: CGFloat = 60 + self.view.safeAreaInsets.bottom
//Button
let button = UIButton(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.height - height, width: UIScreen.main.bounds.width, height: height))
button.setTitle("Save", for: .normal)
button.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
button.contentHorizontalAlignment = .right
button.contentVerticalAlignment = .top
button.titleEdgeInsets.top = 10
button.titleEdgeInsets.right = 10
button.addTarget(self, action: #selector(onTapSaveButton), for: .touchUpInside)
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
//Add constraints
NSLayoutConstraint.activate([
self.view.leadingAnchor.constraint(equalTo: button.leadingAnchor),
button.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
button.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
button.heightAnchor.constraint(equalToConstant: height)
])
}
您可以轻松地在storyboard/subclassing 中做同样的事情。我觉得这个比上一个好。
方法 3:
子类UIButton 并使用它以编程方式创建您的按钮。
class CustomButton: UIButton {
override func draw(_ rect: CGRect) {
self.contentHorizontalAlignment = .right
self.contentVerticalAlignment = .top
self.titleEdgeInsets.top = 10
self.titleEdgeInsets.right = 10
}
}