我有这段代码,当我取消注释该块时,按钮消失了,并显示了注释。When uncommented
var fab:UIButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
self.title = "Maintain My Vehicle"
let vehicleInfoController = VehicleInfoController()
vehicleInfoController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
let vehicleLogsController = VehicleLogsController()
vehicleLogsController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 2)
let viewControllerList = [ vehicleInfoController, vehicleLogsController ]
viewControllers = viewControllerList.map { UINavigationController(rootViewController: $0) }
floatingButton()
let centerLabel = UILabel()
// Don't forget this!
centerLabel.translatesAutoresizingMaskIntoConstraints = false
centerLabel.text = "Perfectly centered!"
view.addSubview(centerLabel)
NSLayoutConstraint.activate([
centerLabel.centerXAnchor.constraint(
equalTo: view.centerXAnchor, constant: 0),
centerLabel.centerYAnchor.constraint(
equalTo: view.centerYAnchor, constant: 0)
])
/*
fab.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
fab.centerXAnchor.constraint(
equalTo: view.centerXAnchor, constant: 0),
fab.centerYAnchor.constraint(
equalTo: view.bottomAnchor, constant: 0)
])
*/
}
func floatingButton(){
fab = UIButton(type: .custom)
fab.frame = CGRect(x: 150, y: 600, width: 75, height: 75)
fab.setTitle("+", for: .normal)
fab.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
fab.clipsToBounds = true
fab.layer.cornerRadius = 50
fab.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
fab.layer.borderWidth = 3.0
fab.layer.masksToBounds = true
fab.layer.zPosition = 1
fab.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
view.addSubview(fab)
}
我不知道为什么中心标签很好,但是我的按钮弄乱了。
答案 0 :(得分:2)
您的问题是fab.centerYAnchor.constraint(equalTo: view.bottomAnchor)
,即使bottomAnchor确实符合NSLayoutYAxisAnchor
,但我没有看到这种组合,相反,centerY与另一个centerY锚点相连。
您可以做的是将按钮嵌入视图中,并从centerLabel.bottomAnchor和view.bottomAnchor约束该视图,如下所示:
func floatingButton(centerLabel: UIView) {
let fabView = UIView()
fabView.backgroundColor = .red
fabView.layer.zPosition = 1
fab = UIButton(type: .custom)
fab.setTitle("+", for: .normal)
fab.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
fab.clipsToBounds = true
fab.layer.cornerRadius = 50
fab.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
fab.layer.borderWidth = 3.0
fab.layer.masksToBounds = true
fabView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(fabView)
fab.translatesAutoresizingMaskIntoConstraints = false
fabView.addSubview(fab)
NSLayoutConstraint.activate([
centerLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
centerLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0),
fabView.topAnchor.constraint(equalTo: centerLabel.bottomAnchor),
fabView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
fabView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
fabView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
fab.centerXAnchor.constraint(equalTo: fabView.centerXAnchor, constant: 0),
fab.centerYAnchor.constraint(equalTo: fabView.centerYAnchor, constant: 0),
fab.widthAnchor.constraint(equalToConstant: 75),
fab.heightAnchor.constraint(equalToConstant: 75),
])
}
以下只是清理过的viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Maintain My Vehicle"
self.view.backgroundColor = UIColor.white
let vehicleInfoController = UIViewController()
vehicleInfoController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
let vehicleLogsController = UIViewController()
vehicleLogsController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 2)
let viewControllerList = [ vehicleInfoController, vehicleLogsController ]
self.viewControllers = viewControllerList.map { UINavigationController(rootViewController: $0) }
let centerLabel = UILabel()
centerLabel.text = "Perfectly centered!"
centerLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(centerLabel)
floatingButton(centerLabel: centerLabel)
}
产生