I'm trying to find a good way to add simple UIButton subview with an action to some UIViewControllers.
I think it could be working with protocol and its extension, but the extension does not allow the @objc methods, It means, I can't add target with Selector.
I created another class to solve this @objc problem, but I could't add the view as a parameter...
I also created an UIViewController subclass and it's working perfectly, but I'm not sure that it is a good idea to implement a lot UIViewControllers with that subclass.
So the question is, what is the best solution to add subviews with action to more than one UIViewController?
see my code below:
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)
}
}