【发布时间】:2018-08-01 09:14:21
【问题描述】:
我正在尝试找到一种向某些 UIViewController 添加带有操作的简单 UIButton 子视图的好方法。
我认为它可以使用协议及其扩展,但扩展 does not allow @objc 方法,这意味着我无法使用 Selector 添加目标。
我创建了另一个类来解决这个@objc 问题,但我无法将视图添加为参数...
我还创建了一个 UIViewController 子类,它运行良好,但我不确定使用该子类实现大量 UIViewController 是否是个好主意。
所以问题是,将带有动作的子视图添加到多个 UIViewController 的最佳解决方案是什么?
请参阅下面的代码:
public protocol ImplementNewRightIcon {
func setupNewBottomRightMenu()
}
public extension ImplementNewRightIcon where Self: UIViewController {
func setupNewBottomRightMenu() {
let buttonwith: CGFloat = 50
let button: UIButton = {
let btn = UIButton()
btn.setImage(#imageLiteral(resourceName: "icons8-plus-math-50_white"), for: .normal)
btn.backgroundColor = .red
btn.clipsToBounds = true
btn.layer.cornerRadius = buttonwith * 0.5
btn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10)
btn.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
btn.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
btn.layer.shadowOpacity = 0.5
btn.layer.shadowRadius = 0.0
btn.layer.masksToBounds = false
btn.addTarget(SetupMenuLayer.sharedInstance, action: #selector(SetupMenuLayer.showMenuButtonDidTapped(SenderViewController:)), for: .touchUpInside)
return btn;
}()
self.view.addSubview(button)
button.anchor(top: nil, left: nil, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 20, paddingRight: 20, width: buttonwith, height: buttonwith)
}
}
class SetupMenuLayer: NSObject {
static let sharedInstance = SetupMenuLayer()
@objc func showMenuButtonDidTapped(SenderViewController: UIViewController) {
let bottomMenuView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
SenderViewController.view.addSubview(bottomMenuView)
bottomMenuView.anchor(top: nil, left: SenderViewController.view.leftAnchor, bottom: SenderViewController.view.bottomAnchor, right: SenderViewController.view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: SenderViewController.view.frame.width, height: SenderViewController.view.frame.height / 3)
}
}
class ImplementNewRightIconView: UIViewController {
override func viewDidLoad() {
self.setupNewRightIconBtn()
}
func setupNewRightIconBtn() {
let buttonwith: CGFloat = 50
let button: UIButton = {
let btn = UIButton()
btn.setImage(#imageLiteral(resourceName: "icons8-plus-math-50_white"), for: .normal)
btn.backgroundColor = .red
btn.clipsToBounds = true
btn.layer.cornerRadius = buttonwith * 0.5
btn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10)
btn.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
btn.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
btn.layer.shadowOpacity = 0.5
btn.layer.shadowRadius = 0.0
btn.layer.masksToBounds = false
//it works
btn.addTarget(self, action: #selector(showMenuButtonDidTapped), for: .touchUpInside)
return btn;
}()
self.view.addSubview(button)
button.anchor(top: nil, left: nil, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 20, paddingRight: 20, width: buttonwith, height: buttonwith)
}
@objc func showMenuButtonDidTapped() {
let bottomMenuView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
self.view.addSubview(bottomMenuView)
bottomMenuView.anchor(top: nil, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: self.view.frame.width, height: self.view.frame.height / 3)
}
}
【问题讨论】:
-
你试过用'@objc extension'代替'@objc func'吗?
-
是的,我做到了,'@objc' 只能应用于类的扩展