iOS 10 系统下UISearchBar编辑超过textField宽度过多文字时 文字下移

iOS 10 系统下UISearchBar编辑超过textField宽度过多文字时 文字下移问题。
正常:


iOS 10 系统下UISearchBar编辑超过textField宽度过多文字时 文字下移_第1张图片
74CA1C95762A55E7F4758F6516F6C472.png

下移:


iOS 10 系统下UISearchBar编辑超过textField宽度过多文字时 文字下移_第2张图片
F2401703961C088C201F755A6C4B7CE9.png

最近遇到一个奇葩问题,UISearchBar设置的导航栏在iOS 10系统下的手机上莫名的出现输入文字过多出现明显文本下移的问题而其他系统下是正常的,然后设置了UISearchBar相关属性都无效,在断点调试UISearchBar中的textField的代理方法发现当文本输入过多时textField的frame往下偏移了3个距离。遍历UISearchBar中的textField拿出后设置文本居中和网上单独解决UITextField文本的方法发现对UISearchBar还是无效,最后在反复调试下还是解决了这个问题。下面是详细的解决方法:

因为为了解决iOS 11 UISearchBar默认的placeholder文本居左的问题 所以顺便自定义UISearchBar。具体方法如下:

#import "MMBaseSearchBar.h"

@interface MMBaseSearchBar : UISearchBar

//修复iOS 10 删除文字光标下移
+(void)sendSearchBar:(UISearchBar *)searchBar

@end
#import "MMBaseSearchBar.h"

@interface MMBaseSearchBar () 

@property (nonatomic, assign) CGFloat placeholderWidth;

@end

@implementation JPBaseSearchBar

static CGFloat const searchIconW = 20.0;

static CGFloat const iconSpacing = 10.0;

static CGFloat const placeHolderFont = 15.0;

//适配iOS 11 serchBar
- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *view in [self.subviews lastObject].subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *field = (UITextField *)view;
            CGRect rect = CGRectMake(15.0, (self.frame.size.height - 32.0)/2, self.frame.size.width - 30.0, 32.0);
            field.frame = rect;
            field.font = [UIFont systemFontOfSize:14];
            UITextField *baseField = [[UITextField alloc] initWithFrame:rect];
            field = baseField;//修复iOS 10 输入文字很多时 文字位置下移
            [field setBackgroundColor:[UIColor whiteColor]];
            field.textColor = kRGB(51.0, 51.0, 51.0, 1);
            field.borderStyle = UITextBorderStyleNone;
            field.layer.cornerRadius = 2.0f;
            field.layer.masksToBounds = YES;
            [field setValue: kRGB(156.0, 156.0, 156.0, 1) forKeyPath:@"_placeholderLabel.textColor"];
            [field setValue:[UIFont systemFontOfSize:placeHolderFont] forKeyPath:@"_placeholderLabel.font"];
            if (@available(iOS 11.0, *)) {
                // 先默认居中placeholder
                [self setPositionAdjustment:UIOffsetMake((field.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
            }
        }
    }
}
//修复iOS 10 删除文字光标下移
+(void)sendSearchBar:(UISearchBar *)searchBar
{
    for (UIView *view in [searchBar.subviews lastObject].subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *field = (UITextField *)view;
            for (UIScrollView *view in field.subviews) {
                if ([view isKindOfClass:[UIScrollView class]]) {
                    CGPoint offset = view.contentOffset;
                    if (offset.y != 0) {
                        offset.y = 0;
                        view.contentOffset = offset;
                    }
                    break;
                }
            }
        }
    }
}

// 开始编辑的时候重置为靠左
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if ([self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        [self.delegate searchBarShouldBeginEditing:self];
    }
    if (@available(iOS 11.0, *)) {
        [self setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
    }
    return YES;
}
// 结束编辑的时候设置为居中
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]) {
        [self.delegate searchBarShouldEndEditing:self];
    }
    if (@available(iOS 11.0, *)) {
        if (textField.text.length > 0) {
            return YES;
        }else{
            [self setPositionAdjustment:UIOffsetMake((textField.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
        }
    }
    return YES;
}

// 计算placeholder、icon、icon和placeholder间距的总宽度
- (CGFloat)placeholderWidth {
    if (!_placeholderWidth) {
        CGSize size = [self.placeholder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:placeHolderFont]} context:nil].size;
        _placeholderWidth = size.width + iconSpacing + searchIconW;
    }
    return _placeholderWidth;
}

以上是自定义UISearchBar,在自定义UISearchBar的UITextField发现当时解决了UISearchBar输入文字过多下移的问题但是又发现了在删除文字时又出现下移的问题,所以在尝试sendSearchBar:(UISearchBar *)searchBar这个遍历纠正UITextField的UIScrollView的偏移量 开始是打算在UITextField的代理方法中执行纠正方法,但发现并没有解决效果,最后在UISearchBar的代理方法中searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText执行才有效解决此问题。所以为了省时间直接写了个类方法调取。

#pragma mark ---UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (@available(iOS 10.0, *)) {
        [JPBaseSearchBar sendSearchBar:searchBar];
    }
}

以上就是解决iOS 10下UISearchBar文本偏移的问题,如果有人遇到此问题 有更好的解决方法麻烦请告知下,谢谢!

你可能感兴趣的:(iOS 10 系统下UISearchBar编辑超过textField宽度过多文字时 文字下移)