0x00 自定義
導航條上的按鈕,可以通過自定義視圖的方式來創建
然而,有趣的事情發生了…
測驗環境:
Xcode: Version 11.7 (11E801a)
真機
0x01 通過 UIButton 創建
直接上代碼:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:({
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 40, 25);
button.layer.cornerRadius = 12.5;
button.backgroundColor = [UIColor brownColor];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button setTitle:@"中秋" forState:0];
[button setTitleColor:[UIColor whiteColor] forState:0];
button;
})];
效果是這樣的:
5s,iOS12.4.4

Xr,iOS13.7

代碼中按鈕的高度設定的是 25
然而,實際效果卻被改變成 29
這特么的,蛋疼Q_Q
0x02 通過 UIControl 創建
直接上代碼:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:({
UIControl *control = [[UIControl alloc] init];
control.frame = CGRectMake(0, 0, 40, 25);
control.layer.cornerRadius = 12.5;
control.backgroundColor = [UIColor orangeColor];
UILabel *label = [[UILabel alloc] init];
label.frame = control.bounds;
label.text = @"快樂";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:14];
label.textAlignment = NSTextAlignmentCenter;
[control addSubview:label];
control;
})];
效果是這樣的:
5s,iOS12.4.4

Xr,iOS13.7

這回算是正確了 😃 😉
0x03 總結一下
如果要設定按鈕的背景顏色
UIButton 就有可能不滿足需要
除非最小高度是 29
否則,還是使用 UIControl 吧
五筆學習-86版98版
就是這么簡單~

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/148652.html
標籤:其他
上一篇:centos6虛擬機安裝到開始使用程序指引&LAMP
下一篇:iOS14剪切板適配案例分析:淘寶是如何判斷剪貼板內容是淘口令?(通過判斷是否符合數字和鏈接的規則來判斷是否命中淘口令的格式)
