gpt4 book ai didi

swift - 添加多个 UIViewControllers subview 的最佳方法

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:55 24 4
gpt4 key购买 nike

我正在尝试找到一种向某些 UIViewControllers 添加带有操作的简单 UIButton subview 的好方法。

我认为它可以使用协议(protocol)及其扩展,但扩展 does not allow @objc 方法,这意味着,我无法使用选择器添加目标。

我创建了另一个类来解决这个@objc 问题,但我无法将 View 添加为参数...

我还创建了一个 UIViewController 子类,它运行良好,但我不确定用该子类实现大量 UIViewController 是否是个好主意。

所以问题是,向多个 UIViewController 添加具有操作的 subview 的最佳解决方案是什么?

请看下面我的代码:

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)

}

最佳答案

您可以对 UIButton 进行扩展并添加具有以下语义的方法:

func addTergetClosure(didTouch: () -> Void)

didTouch 将在 touchUpInside 事件中调用,详情 here你怎么能做到。接下来,您只需要像您一样为 UIViewController 制定一个具有 self 约束的协议(protocol),这会将您的按钮添加为 subview 。我将添加按钮属性作为协议(protocol)部分,因为您可能需要在那些 View Controller 中的某处访问它,而不仅仅是添加将在 touchUpInside 上调用的按钮和闭包。

所以它会是这样的:

protocol MyProtocol { 
var button: UIButton { get set }
func setButton()
}

extension MyProtocol where Self: UIViewController {
func setButton() { ... }
}

如果您的按钮在 ViewController 上看起来很相似,那么我建议您将该逻辑分离到带有静态字段的 UIButton 扩展,这将返回带有您需要的样式的 UIButton。在提供的解决方案中,您只需在 ViewController 中设置按钮,如

class MyController: UIViewController, MyProtocol {
var button = UIButton.shadowed
}

extension UIButton {
static let shadowed: UIButton = { ... }
}

关于swift - 添加多个 UIViewControllers subview 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51629518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com