iOS 7 中的 UISearchBar 外观问题

Posted

技术标签:

【中文标题】iOS 7 中的 UISearchBar 外观问题【英文标题】:UISearchBar appearance issues in iOS 7 【发布时间】:2013-10-14 08:09:30 【问题描述】:

我在 UISearchBar 中遇到了 ios 问题。我正在使用以下代码来自定义 UISearchBar :

- (void)showSearchBarCustomColorAndDesignWithTargetForiOS7:(id)target
    UIView * subview;
    NSArray * subviews = [self subviews];

    for (subview in subviews)
        for (UIView *level2SubView in subview.subviews)
            if ([level2SubView isKindOfClass:[UITextField class]])
            
                [((UITextField*)level2SubView) setEnablesReturnKeyAutomatically:NO];
                ((UITextField*)level2SubView).delegate=target;
                [((UITextField*)level2SubView) setEnabled:TRUE];
                ((UITextField*)level2SubView).borderStyle = UITextBorderStyleLine;
                ((UITextField*)level2SubView).textColor = [UIColor whiteColor];
                ((UITextField*)level2SubView).font =[UIFont fontWithName:@"ProximaNova-Light" size:24.0];
                ((UITextField*)level2SubView).autocorrectionType = UITextAutocorrectionTypeNo;

                ((UITextField*)level2SubView).layer.borderWidth = 1.0;
                ((UITextField*)level2SubView).layer.borderColor = UIColorFromRGB(0x2774A7).CGColor;
                [[((UITextField*)level2SubView) valueForKey:@"textInputTraits"] setValue:[UIColor whiteColor] forKey:@"insertionPointColor"];
                ((UITextField*)level2SubView).autocapitalizationType = UITextAutocapitalizationTypeNone;
                break;

            
            if ([level2SubView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            
                [level2SubView removeFromSuperview];
            
        
    

    self.delegate = target;
    [self setImage:[UIImage imageNamed:@"search_white.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:@"delete_white.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
    [self setBackgroundColor:UIColorFromRGB(0x2774A7)];

每当我第一次点击 UISearchBar 时,它都不会调用 searchBarShouldBeginEditing。它也没有显示我输入的文本。但是,当我第二次单击时,它的行为正常。此外,它显示了一些黑色补丁。我的搜索栏是这样的:

【问题讨论】:

【参考方案1】:
This issue may be by adding this code 

[[searchBar.subviews objectAtIndex:0] removeFromSuperview]; or

if ([level2SubView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            
                [level2SubView removeFromSuperview];
            

Please remove this and see.

【讨论】:

以上是关于iOS 7 中的 UISearchBar 外观问题的主要内容,如果未能解决你的问题,请参考以下文章

iOS 13 UISearchBar 外观和行为

ios 7中yelp和foursquare如何将UISearchBar中的搜索图标保持在左侧

字段为空时为 UISearchBar 启用“搜索”按钮? (IOS 7)

iOS 7 UISearchBar 右间距

UITextfield 在 iOS 7 中不能作为 UISearchBar 的子视图?

UISearchBar 在 iOS 7 中隐藏视图