【问题标题】:appearance().setBackgroundImage Not Working On Custom Class外观()。setBackgroundImage 不适用于自定义类
【发布时间】:2021-02-06 05:19:28
【问题描述】:

我创建了一个 UIBarButtonItem 自定义类,并在情节提要中为该类分配了一个条形按钮项。

在应用程序委托中,我尝试使用以下方法为其设置外观:

VIPButton.appearance().setBackgroundImage(UIImage(named: "vipButton"), for: .normal, barMetrics: .default)

但是,虽然这适用于常规 UIBarButtonItems,但它对我的自定义类栏按钮项没有影响。

任何帮助将不胜感激。

【问题讨论】:

    标签: swift uibarbuttonitem


    【解决方案1】:

    从 UIButton 创建自定义类:

    public class SimpleButton: UIButton {
    
        @objc dynamic var backColor: UIColor? {
            didSet {
                self.backgroundColor = self.backColor
            }
        }
        
        @objc dynamic var image: UIImage? {
            didSet {
                self.setImage(self.image, for: .normal)
            }
        }
    }
    

    接下来,您从 UIBarButtomItem 创建一个类。 现在将 customView 属性设置为 SimpleButon 以获得自定义外观并为此设置操作,如下所示:

    public class SimpleBarButton: UIBarButtonItem {
        
        // create object from simpleButton
        private let button = SimpleButton()
        
        public required init?(coder: NSCoder) {
            super.init(coder: coder)
                    
            // add target for button
            self.button.addTarget(self, action: #selector(self.buttonTapped(_:)), for: .touchUpInside)
            
            // set title
            self.button.setTitle("title", for: .normal)
            self.button.sizeToFit()
            
            // assing simple button to customView property
            self.customView = self.button
        }
        
        // get callback from touchUp button
        @objc func buttonTapped(_ sender: UIButton) {
            
            // here set callback for tapped on BarButtonItem
            // check target and action is not Nil
            guard let target = self.target, let action = self.action else {
                return
            }
            
            // call selector (implement to your ViewController)
            // pass self for parameters
            target.performSelector(inBackground: action, with: self)
        }
    }
    

    我在本节中创建了一个类,并为 customView 变量分配了一个按钮。

    现在我像这样向 SimpleButton 添加样式:

    SimpleButton.appearance().backColor = .red
    SimpleButton.appearance().image = UIImage.init(named: "images")!
    

    现在在情节提要上创建 BarButtonItem 并将自定义类更改为 SimpleBarButton:

    现在创建 ViewController 并为事件 barButton 添加操作:

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        
        @IBAction func sampleClicked(_ sender: SimpleBarButton) {
            print("callBack from action on BarButton Item")
        }
    }
    

    并将此函数分配给 BarButton:

    界面输出:

    【讨论】:

    • 这对 UIBarButtonItem 没有影响,您是否为 UIBarButtonitem 自己尝试过?
    • 你好,我的朋友。编辑我的答案并使用 UIBarBttonItem 创建我希望它有用@dub
    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多