【问题标题】:Swift: Set UIView as the background of UIBarButton/UIButtonSwift:将 UIView 设置为 UIBarButton/UIButton 的背景
【发布时间】:2019-08-09 15:08:46
【问题描述】:

如何将UIView 设置为UIBarButton 的背景? UIBarButton/UIButton 应该包含它的标题和背景图片。

我尝试覆盖 UIButton 类,但它不起作用。如果有人有解决方案,请告诉我。

【问题讨论】:

    标签: ios swift uibutton uibarbuttonitem


    【解决方案1】:

    也许您应该使用 UIImageView.image 而不是 UIView 本身来设置底部背景图像。

    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var myButton: UIButton!
        @IBOutlet weak var myView: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            myButton.setBackgroundImage(myView.image, for: .normal)
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用继承自 UIControl 的 CustomButton。通过使用 xib,您可以根据您的要求管理您的 UI。您可以在其中添加 n 个 UI 元素。你会得到像 UIButton 这样的默认事件处理程序(比如 touchupinside、touchupoutside、valuechange)。您可以提供相同的 IBActions。附上一些屏幕截图供参考。如果您在执行此操作时遇到任何问题,请告诉我。继续编码。

      【讨论】:

        【解决方案3】:
        // For UIBarButtonItem (just set customView parameter)
        
        let frame = CGRect(x: 0, y: 0, width: 100, height: 40)
        let customView = UIView(frame: frame)
        customView.backgroundColor = .red
        
        let barButtonItem = UIBarButtonItem(customView: customView)
        
        // for UIButton
        
        // 1. Subview case (add subview on your target UIButton, send to back so that it does not cover title)
        
        let button0 = UIButton(frame: frame)
        button0.addSubview(customView)
        button0.sendSubviewToBack(customView)
        button0.setTitle("Button", for: UIControl.State())
        
        // 2. Rendered image case (as an alternative render your view to an image and add as a background image)
        
        // Extension for capturing a UIVIew as an image:
        extension UIImage {
            convenience init?(view: UIView) {
                UIGraphicsBeginImageContext(view.frame.size)
                guard let ctx = UIGraphicsGetCurrentContext() else {
                    return nil
                }
                view.layer.render(in: ctx)
                let image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                guard let cgImage = image?.cgImage else {
                    return nil
                }
                self.init(cgImage: cgImage)
            }
        }
        
        let button1 = UIButton(frame: frame)
        let customViewImage = UIImage.init(view: customView)
        button1.setBackgroundImage(customViewImage, for: UIControl.State())
        button1.setTitle("Button", for: UIControl.State())
        

        【讨论】:

          【解决方案4】:

          您可以将视图添加为按钮的subView

          extension UIButton {
              func setBackgroundView(view: UIView) {
                  view.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
                  view.isUserInteractionEnabled = false
                  self.addSubview(view)
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2019-10-13
            • 1970-01-01
            • 2015-01-24
            • 2021-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多