在左侧导航栏上添加图像和文本

时间:2013-08-30 10:56:57

标签: ios objective-c uinavigationbar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewController];

// CHANGE COLOR TO BLACK

nav.navigationBar.tintColor = [UIColor blackColor];

// ADDING IMAGE TO NAVIGATION BAR
UIImage *image = [UIImage imageNamed:@"logo_36.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[nav.navigationBar.topItem setTitleView:imageView];

self.window.rootViewController = nav;
[self.window makeKeyAndVisible];  

return YES;
}

我在appdelegate类中尝试过这个代码,因为我想在整个项目中使用它,它适用于显示图像,但它显示在中心也需要我无法显示的文本。

请有人建议如何在iOS

的导航栏左侧显示图像和文字

1 个答案:

答案 0 :(得分:-1)

为什么要将imageview设置为导航topItem titleview

替换

[nav.navigationBar.topItem setTitleView:imageView];

nav.navigationbar.topItem.leftBarButtonItem:imageview

并设置`[nav.navigationBar.topItem setTitle:@“你想要的文字”];

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarButtonItem=mailbutton;
[someButton release];