
搜索頁(yè)面
一、問(wèn)題描述
通過(guò)以下設(shè)置在ios13之前有效,在ios13之后會(huì)造成設(shè)置字體大小失效,
for (UIView *subView in [[searchBar.subviews lastObject] subviews]) {
if ([[subView class] isSubclassOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)subView;
textField.font = [UIFont systemFontOfSize:14];
_searchTextField = textField;
break;
}
}
因?yàn)榇蛴earchBar.subviews.lastObject.subviews在ios13之前:
<__NSArrayM 0x2818fc210>(
<UISearchBarBackground: 0x1064f6160; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x281617240>>,
<UISearchBarTextField: 0x10681e000; frame = (0 0; 0 0); text = ''; opaque = NO; layer = <CALayer: 0x2816145e0>>
)
在ios13之后:
<__NSArrayM 0x282d96940>(
<UISearchBarBackground: 0x1063c5500; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x282300f00>>,
<_UISearchBarSearchContainerView: 0x1063c5920; frame = (0 0; 0 0); autoresize = W; gestureRecognizers = <NSArray: 0x282d76d30>; layer = <CALayer: 0x282301400>>
)
所以if里面的設(shè)置不再執(zhí)行。
二、解決方法
在ios13之后UISearchBar暴露了UITextField屬性,這點(diǎn)算是蘋(píng)果的優(yōu)化,方便取了,但是你搞下兼容啊......
現(xiàn)在如下設(shè)置:
if (@available(iOS 13.0, *)) {
UITextField *tf = searchBar.searchTextField;
tf.font = [UIFont systemFontOfSize:14];
} else {
for (UIView *subView in [[searchBar.subviews lastObject] subviews]) {
if ([[subView class] isSubclassOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)subView;
textField.font = [UIFont systemFontOfSize:14];
_searchTextField = textField;
break;
}
}
}