在ios中设置UINavigationitem(左栏按钮项)的图像

时间:2014-05-15 03:23:09

标签: ios uinavigationcontroller uibarbuttonitem uinavigationitem

我正在尝试使用以下代码为UIBarButtonItem设置图像。但它在导航栏中显示为空白图像。请帮我解决问题。我是ios的新手。

[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];

提前谢谢你.......

3 个答案:

答案 0 :(得分:1)

您无需直接使用导航栏。使用当前视图控制器的导航项属性来设置UIBarButtomItems。确保您实际上正在使用navigationController来设置这些属性。

答案 1 :(得分:1)

 UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(popToBack)];      //create the left bar button

 self.navigationItem.leftBarButtonItem = leftBarButton;   //assign into navigation item

 self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1];   //set the tint color

 [leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor],  UITextAttributeTextColor,nil] forState:UIControlStateNormal];    //text color


-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
 }

在另一个选择中

UIButton *leftbutton =  [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:@selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;

-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
 }

答案 2 :(得分:1)

UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:@"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:@selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;