0

デフォルトの検索バーの境界線を削除し、プレーンな検索バーを実装したいと考えています。下の画像を参照してください

デフォルトの検索バー、

デフォルトの検索バー

これが私が達成しようとしていることです。

プレーン検索バー

私はググって以下のコードを試しましたが、望ましい効果が得られません。

for (id img in searchbar.subviews) 
{
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {
        [img removeFromSuperview];
    }
}

searchbar.delegate = self;
searchbar.layer.borderWidth = 0;
searchbar.layer.borderColor = [[UIColor clearColor] CGColor];

どうすればこれを実装できますか?

4

2 に答える 2

1

回答ありがとうございます。以下のコードを使用して問題を解決しました....

以下のコードで使用されている無地の白い検索バーの背景画像

UITextField *txfSearchField = [self.searchbar valueForKey:@"_searchField"];
        [txfSearchField setBackgroundColor:[UIColor whiteColor]];
        [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
        [txfSearchField setRightViewMode:UITextFieldViewModeNever];
        [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
        [txfSearchField setBorderStyle:UITextBorderStyleNone];
        //txfSearchField.layer.borderWidth = 8.0f;
        //txfSearchField.layer.cornerRadius = 10.0f;
        txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
        txfSearchField.clearButtonMode=UITextFieldViewModeNever;

        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:20]];
        [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];
于 2013-10-21T15:05:56.147 に答える
0

iOS 7 では小さな変更があり、2 つのレベルを反復する必要があります。

 for (UIView *subView in self.searchBar.subViews){
    for (UIView *2ndLeveSubView in subView.subViews){
    if ([2ndLevelSubView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [2ndLevelSubView removeFromSuperView];
        }
    }
   }
于 2013-10-15T08:10:57.053 に答える