swift - 以编程方式使用约束 Swift 移动按钮
问题描述
@objc func buttonRoundPlayer(){
view?.addSubview(buttonRound)
buttonRound.setTitle("Jump", for: .normal)
buttonRound.addTarget(self, action: #selector(roundhandle), for: .touchUpInside)
buttonRound.backgroundColor = .red
buttonRound.layer.cornerRadius = 5
buttonRound.layer.borderWidth = 1
buttonRound.layer.borderColor = UIColor.white.cgColor
buttonRound.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),buttonRound.widthAnchor.constraint(equalToConstant: 100),buttonRound.heightAnchor.constraint(equalToConstant:50)])
}
我最近设法在理解按钮的布局并使其工作方面获得帮助,但我不知道如何调整约束以将按钮从屏幕底部的边缘移开。
预先感谢您的帮助。
解决方案
不知道底边的边缘是什么意思,如果您在谈论安全区域布局指南,那么您可以使用
NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo:view!.safeAreaLayoutGuide.bottomAnchor),
buttonRound.widthAnchor.constraint(equalToConstant: 100),
buttonRound.heightAnchor.constraint(equalToConstant:50)])
您的代码中有几个问题,
- 您两次应用了相同的约束,这对我来说没有逻辑意义
buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),
buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor)
您使用
view!
不确定是否是 ViewController 的视图来强制解包视图,如果它是 ViewController 的视图,则无需将其强制解包,因为它本质上是隐式可选的,因此您应该可以改写view.safeAreaLayoutGuide
。在整个代码中,您访问视图就像它是可选的一样,使用类似的语句
view?.addSubview(buttonRound)
,view!.bottomAnchor
因为我不确定它是哪个视图,如果您非常确定它是可选的,我建议使用 safe unwrap withif let
,guard let
而不是!
if let view = view {
view.addSubview(buttonRound)
buttonRound.setTitle("Jump", for: .normal)
buttonRound.addTarget(self, action: #selector(roundhandle), for: .touchUpInside)
buttonRound.backgroundColor = .red
buttonRound.layer.cornerRadius = 5
buttonRound.layer.borderWidth = 1
buttonRound.layer.borderColor = UIColor.white.cgColor
buttonRound.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor),
buttonRound.widthAnchor.constraint(equalToConstant: 100),
buttonRound.heightAnchor.constraint(equalToConstant:50)])
}
编辑:正如下面的 OP 所评论的,他看到了错误
“SafeAreLayoutGude”仅适用于 iOS 11.0 或更新版本
OP 必须使用低于 iOS 11 的部署目标,并且因为 OP 没有在评论中回复我的问题,所以我正在更新答案以支持低于 iOS 11.0
if let view = view {
view.addSubview(buttonRound)
buttonRound.setTitle("Jump", for: .normal)
buttonRound.addTarget(self, action: #selector(roundhandle), for: .touchUpInside)
buttonRound.backgroundColor = .red
buttonRound.layer.cornerRadius = 5
buttonRound.layer.borderWidth = 1
buttonRound.layer.borderColor = UIColor.white.cgColor
buttonRound.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([buttonRound.widthAnchor.constraint(equalToConstant: 100),
buttonRound.heightAnchor.constraint(equalToConstant:50)])
if #available(iOS 11.0, *) {
buttonRound.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
else {
buttonRound.bottomAnchor.constraint(equalTo:view.bottomAnchor).isActive = true
}
}
不太确定你正在构建/维护什么样的应用程序,iOS 11 对我来说似乎太旧了,检查你是否真的需要支持这么旧的 iOS 版本,在你的项目设置中更改你的 iOS 部署目标值以避免多个兼容诸如此类的问题。
推荐阅读
- use-case - 用例图“详细信息”选项卡显示“无详细信息”
- c# - 将业务逻辑与 API 解耦——如何解耦辅助类
- laravel - Laravel Office365 预期响应代码 250 但得到代码“”,带有消息“”
- python - 如何在python中使用scrapy将自定义信息添加到json文件
- arrays - BigQuery argmax:进行 CROSS JOIN UNNEST 时是否保持数组顺序
- java - 基于条件和顺序的 Java 8 lambda 过滤
- c++ - 更新 TJSONArray
- javascript - 如何将“e.target.id”用作 for 循环中的索引?
- kml - 如何将kml文件转换为pdf
- r - order Plotly 条形图 wrt Y 轴/按条形高度