我总是有问题说:无法转换UIImage类型的值?.Type'到预期的参数类型'UIImage?'
我在下面使用了以下代码:
btn.setBackgroundImage(UIImage?: welcome.png ,for : UIControlState.normal, barMetrics: UIBarMetrics.default)
答案 0 :(得分:2)
试试这个:
btn.setBackgroundImage(UIImage(named: "welcome") ,for : UIControlState.normal)
答案 1 :(得分:0)
<强>的OBJ-C:强>
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 40.0f)];
[btn addTarget:self action:@selector(buttonEN:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:@"cys_lang_en.png"] forState:UIControlStateNormal];
UIBarButtonItem *eng_btn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = eng_btn;
Swift 4.0:
var btn = UIButton(type: .custom)
btn.frame = CGRect(x: 0.0, y: 0.0, width: 25.0, height: 40.0)
btn.addTarget(self, action: Selector("buttonEN:"), for: .touchUpInside)
btn.setImage(UIImage(named: "cys_lang_en.png"), for: .normal)
var eng_btn = UIBarButtonItem(customView: btn)
navigationItem?.rightBarButtonItem = eng_btn
答案 2 :(得分:0)
btn.setBackgroundImage(UIImage(named: "welcome.png"), for: .normal, barMetrics: .default)
答案 3 :(得分:0)
Swift 5.0
btn.setBackgroundImage(UIImage(named: "welcome"), for: .normal, barMetrics: .default)