Swift 3 - UICollectionView 上的浮动按钮
Swift 3 - Floating Button over UICollectionView
我通过 this first answer 创建了一个浮动按钮。它有效,但是当 UICollectionView
启动时,浮动按钮仍然是正方形,并在所有数据出现后变成圆形(在 loadAPI 完成后 运行)。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
roundButton.backgroundColor = green
roundButton.clipsToBounds = true
roundButton.setImage(UIImage(named:"ic_add_white_2x"), for: .normal)
roundButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
roundButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
roundButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20),
roundButton.widthAnchor.constraint(equalToConstant: 60),
roundButton.heightAnchor.constraint(equalToConstant: 60)
])
}
@IBAction func ButtonClick(_ sender: UIButton){
//print("clicked")
}
我需要按钮是一个圆圈,因为 UICollectionView
第一次出现。有人可以帮我吗?谢谢!
// 更新此代码
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}
我通过 this first answer 创建了一个浮动按钮。它有效,但是当 UICollectionView
启动时,浮动按钮仍然是正方形,并在所有数据出现后变成圆形(在 loadAPI 完成后 运行)。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
roundButton.backgroundColor = green
roundButton.clipsToBounds = true
roundButton.setImage(UIImage(named:"ic_add_white_2x"), for: .normal)
roundButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
roundButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
roundButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20),
roundButton.widthAnchor.constraint(equalToConstant: 60),
roundButton.heightAnchor.constraint(equalToConstant: 60)
])
}
@IBAction func ButtonClick(_ sender: UIButton){
//print("clicked")
}
我需要按钮是一个圆圈,因为 UICollectionView
第一次出现。有人可以帮我吗?谢谢!
// 更新此代码
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}