目标c中UINavigationBar右侧的UISearchBar

GAM*_*AMA 3 iphone uinavigationbar uisearchbar ipad ios

我创建了一个iPad应用程序,我希望将searchBar放在导航栏的右侧.

当我在导航栏中添加搜索栏时,它位于navigationBar的中间位置.在中间,相反,我想显示一个特定的图像.

我怎样才能做到这一点?

这是我的代码片段,

[self.navigationController.navigationBar setTintColor:[[UIColor alloc]initWithRed:150.0 / 255 green:22.0 / 255 blue:22.0 / 255 alpha:0.0]];

UIImage *image = [UIImage imageNamed:@"logo(1).png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationItem setTitleView:imageView];

self.navigationItem.titleView = sBar;

[titleView release];
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我只能在模拟器中看到searchBar(位于中间).

Mih*_*atu 12

试试这个:

UISearchBar *searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 40)] autorelease];
searchBar.backgroundImage = [[[UIImage alloc] init] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:searchBar] autorelease];
Run Code Online (Sandbox Code Playgroud)

您可以随时更改框架.请记住,该backgroundImage属性仅适用于iOS 5.您应该搜索其他选项以摆脱iOS 4.x的背景

如果这对您有用,请告诉我.