UISearchBar背景颜色与导航栏重叠

时间:2014-10-21 14:27:17

标签: ios uitextfield uinavigationbar uisearchbar

enter image description here

正如您所看到的,我的uisearchbar与导航栏重叠。搜索栏底部还有一条细长的黑色线条。我试过了

_itemSearchBar.searchBarStyle = UISearchBarStyleMinimal;

它修复了外观,但我无法在文本字段中设置背景颜色。

    if(!_itemSearchBar)
    {
        _itemSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
        _itemSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _itemSearchBar.placeholder = @"What are you looking for?";
        _itemSearchBar.delegate = self;
        _itemSearchBar.barTintColor = [UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0];


        UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
        searchBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [searchBarView addSubview:_itemSearchBar];
        self.navigationItem.titleView = searchBarView;

        UITextField* sbTextField;

        for (UIView *subView in _itemSearchBar.subviews){
            for (UIView *ndLeveSubView in subView.subviews){

                if ([ndLeveSubView isKindOfClass:[UITextField class]])
                {

                    sbTextField = (UITextField *)ndLeveSubView;
                    //changes typing text color #cf5300
                    UIColor *color = [UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0];
                    sbTextField.textColor = color;
                    sbTextField.backgroundColor = [UIColor colorWithRed:130/255.0 green:58/255.0 blue:23/255.0 alpha:1.0];
                    //changes placeholder color
                            sbTextField.attributedPlaceholder =
                            [[NSAttributedString alloc]
                             initWithString:@"What are you looking for?"
                             attributes:@{NSForegroundColorAttributeName:color}];

                    break;
                }

            }

        }

0 个答案:

没有答案