美文网首页iOS 11iOS学习iOS项目实践中的学习
iOS11适配 - UINavigationBar上添加UISe

iOS11适配 - UINavigationBar上添加UISe

作者: Jvaeyhcd | 来源:发表于2017-09-22 10:27 被阅读740次

咳咳,最近两天都在适配iOS11和iPhone X,在项目中遇到比较麻烦的一个适配是:在导航栏上添加的UISearchBar在iOS10及一下版本上显示一切正常,但是当我升级到了iOS11后,整个世界都不好了,导航栏上的UISearchBar不见了。

最终我还是成功的再iOS11上将UINavigationBar上的UISearchBar显示出来了,当然解决问题的这个过程还是很艰辛的,翻遍Google,Stack Overflow后终于找到了解决办法。再次记录下吧,希望能够帮助到同样遇到相同问题的各位开发者。

问题展示

先来一张图对比下iOS10和iOS11两个不同系统下,在导航栏中添加UISearchBar的情况,大家先感受下:

iOS11以下版本
iOS11

情况大概就是这么一个情况,在iOS11上导航栏上的搜索框不见了。

接下来来分析一下代码吧!

代码分析

UIView *titleView = [[UIView alloc] init];
titleView.py_x = PYSEARCH_MARGIN * 0.5;
titleView.py_y = 7;
titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
[titleView addSubview:searchBar];
titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleView;

一般来说都是这样将UISearchBar添加到导航栏上去的,先定义一个titleView,将UIsearchBar添加到titleView,然后将titleView赋值给self.navigationItem.titleView。

解决办法

将titleView的UIView重写为XUIView。

#import "XUIView.h"

@implementation XUIView

-(CGSize)intrinsicContentSize
{
    return UILayoutFittingExpandedSize;
}
@end

然后将上面的代码修改为:

UIView *titleView = [[XUIView alloc] init];
titleView.py_x = PYSEARCH_MARGIN * 0.5;
titleView.py_y = 7;
titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
[titleView addSubview:searchBar];
titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleView;

最后在iOS11上导航栏上的UISearchBar就显示出来了~~

相关文章

网友评论

  • 洁简:UISearchBar 放到view上虽然我设置的高度是28 但是里面textField明显变高了 该如何处理
    不用真名_27f1:@Jvaeyhcd [self.searchBar.heightAnchor constraintEqualToConstant:28].active = YES;
    我这样修改了高度,是可以, 但是我修改TextField的圆角半径, 看起来就是不圆, UITextField始终是36,

    - (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    if (searchField) {
    if (@available(iOS 11.0, *)) {
    [self.textField.heightAnchor constraintEqualToConstant:28].active = YES;
    [searchField.heightAnchor constraintEqualToConstant:28].active = YES;
    }
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入要搜索的内容" attributes:
    @{NSForegroundColorAttributeName:
    [UIColor colorWithHex:0xFFFFFF],
    NSFontAttributeName:[UIFont bxg_fontRegularWithSize:13]
    }];
    // NSLog(@"searchFiled=%@")
    searchField.attributedPlaceholder = attrString;
    [searchField setBackgroundColor:[UIColor colorWithHex:0xffffff alpha:0.2]];
    searchField.textColor = [UIColor whiteColor];
    searchField.layer.cornerRadius = 14;// searchField.bounds.size.height * 0.5;
    searchField.layer.masksToBounds = YES;
    }

    }
    Jvaeyhcd:@洁简 这个在iOS11确实变高了,改了高度也没有什么用

本文标题:iOS11适配 - UINavigationBar上添加UISe

本文链接:https://www.haomeiwen.com/subject/yprtextx.html