简体   繁体   English

Swift:将UIView设置为UIBarButton / UIButton的背景

[英]Swift: Set UIView as the background of UIBarButton/UIButton

How can I set a UIView as the background of a UIBarButton ? 如何将UIView设置为UIBarButton的背景? UIBarButton/UIButton should contain its title and background image. UIBarButton/UIButton应包含其标题和背景图像。

I tried with overriding UIButton class, but it doesn't work. 我尝试覆盖UIButton类,但是它不起作用。 If someone has a solution please advise me. 如果有人有解决方案,请告诉我。

You can add view as subView of button: 您可以将视图添加为按钮的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)
    }
}
// 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())

You can use CustomButton which inherits from UIControl. 您可以使用从UIControl继承的CustomButton。 By using xib you canmanage your UI as per your requirements. 通过使用xib,您可以根据需要管理UI。 You can add n number of UI elements in it. 您可以在其中添加n个UI元素。 You will get default event handler with this like UIButton (like touchupinside, touchupoutside, valuechange). 您将获得带有UIButton之类的默认事件处理程序(如touchupinside,touchupoutside,valuechange)。 You can provide IBActions for same. 您可以提供相同的IBAction。 For reference attaching some screen shots. 作为参考,请附上一些屏幕截图。 Let me know if you face any while implementing this. 让我知道您在执行此操作时是否遇到任何困难。 Keep coding. 继续编码。

添加XIB

添加UIControl的子类并管理IBOut

管理事件

Maybe you should use UIImageView.image instead of the UIView itself to set the bottom background image. 也许您应该使用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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM