0

iPhoneアプリで背景画像をUISearchバーに設定しようとしていますが、画像が表示されません。

これが私のコードです

UISearchBar * tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 40 )];

self.searchBar = tempSearchBar;
tempSearchBar.delegate = self; 
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.barStyle = UIBarStyleBlack;
self.searchBar.backgroundColor=[UIColor clearColor];
self.searchBar.backgroundImage=[UIImage imageNamed:@"Searchbox.png"];
self.searchBar.placeholder = @"Search My Data";
[self.searchBar sizeToFit];  

[self.view addSubview:self.searchBar];
[tempSearchBar release];
4

5 に答える 5

8
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];

上記の方法を使用することで、searchBarのbgImageを設定できます

于 2013-03-04T05:56:55.303 に答える
2
[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_bar"] forState:UIControlStateNormal];

Interface Builder は次のコードと同等の機能を提供しますが (ただし、望ましい効果ではありません)

[self.searchDisplayController.searchBar setBackgroundImage:[UIImage imageNamed:@"search_bar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
于 2015-01-22T07:40:50.213 に答える
0
  • (無効) clearSearchBarBg {

for (UIView *searchBarCity.subviews のサブビュー) {

    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
    {
        [subview removeFromSuperview];
        break;
    }

}

for (UIView *subview in searchBarCity.subviews)
{

    if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        [(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
    }
}
for(UIView *subView in searchBarCity.subviews)
{

    if([subView isKindOfClass: [UITextField class]])
    {
        [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];
    }
}

UITextField *searchField = [searchBarCity valueForKey:@"_searchField"];
[searchField setBackground:[UIImage imageNamed:@"SearchInputBox.png"]];// = [UIColor clearColor];

searchField.borderStyle = UITextBorderStyleNone;
[searchField setTextColor:[UIColor blackColor]];

UIImageView* image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
image.image = [UIImage imageNamed:@"navigationBarColorCode.png"];
image.userInteractionEnabled = FALSE;
[searchBarCity insertSubview:image belowSubview:searchField];

searchBarCity.userInteractionEnabled = YES;
searchBarCity.placeholder = @"Search";

}

于 2014-06-25T06:45:38.507 に答える
0

以下のコードを使用

[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"] forState:UIControlStateNormal]; 
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.tintColor=[UIColor orangeColor];
于 2014-12-19T06:20:14.913 に答える