在ios中设置UINavigationitem(Leftbarbutton项)的图像
我正在尝试使用下面的代码为UIBarButtonItem设置图像。但它在导航栏中显示为空白图像。请帮我解决这个问题。我对ios完全陌生在ios中设置UINavigationitem(Leftbarbutton项)的图像,ios,uinavigationcontroller,uibarbuttonitem,uinavigationitem,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
[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];
提前感谢您……您不需要直接使用导航栏。使用当前视图控制器的导航项属性设置UIBarButtomItems。确保您实际使用的是navigationController来设置这些属性。您不需要直接使用导航栏。使用当前视图控制器的导航项属性设置UIBarButtomItems。确保您实际使用的是navigationController来设置这些属性。您不需要直接使用导航栏。使用当前视图控制器的导航项属性设置UIBarButtomItems。确保您实际使用的是navigationController来设置这些属性。您不需要直接使用导航栏。使用当前视图控制器的导航项属性设置UIBarButtomItems。确保您实际使用的是navigationController来设置这些属性
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];
}
另一种选择
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];
}
另一种选择
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];
}
另一种选择
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];
}
你确定你的代码没有问题吗。我认为问题在于你的形象名称。检查一下。图像名称很好。我使用下面的代码实现了输出。UIImage*img=[UIImage ImageName:@“Cross_Icon.png”];UIButton*btn=[UIButton按钮类型:UIButtonTypeCustom];btn.frame=CGRectMake(0,0,img.size.width,img.size.height);[btn setBackgroundImage:img for状态:uicontrol状态正常];[btn addTarget:self action:@selector(Cancelaction)for ControlEvents:UIControlEventTouchUpInside];uibarbuttoneim*lftbtn=[[uibarbuttoneim alloc]initWithCustomView:btn];self.navigationItem.leftBarButtonItem=lftbtn;但是任何人都可以解释为什么不能使用UIBarButtonItem*lftbtn=[[UIBarButtonItem alloc]initWithImage:img样式:uiBarbuttonItemStyleBordedTarget:self action:@selector(cancelaction)];self.navigationItem.leftBarButtonItem=lftbtn;你确定你的代码没有问题吗。我认为问题在于你的形象名称。检查一下。图像名称很好。我使用下面的代码实现了输出。UIImage*img=[UIImage ImageName:@“Cross_Icon.png”];UIButton*btn=[UIButton按钮类型:UIButtonTypeCustom];btn.frame=CGRectMake(0,0,img.size.width,img.size.height);[btn setBackgroundImage:img for状态:uicontrol状态正常];[btn addTarget:self action:@selector(Cancelaction)for ControlEvents:UIControlEventTouchUpInside];uibarbuttoneim*lftbtn=[[uibarbuttoneim alloc]initWithCustomView:btn];self.navigationItem.leftBarButtonItem=lftbtn;但是任何人都可以解释为什么不能使用UIBarButtonItem*lftbtn=[[UIBarButtonItem alloc]initWithImage:img样式:uiBarbuttonItemStyleBordedTarget:self action:@selector(cancelaction)];self.navigationItem.leftBarButtonItem=lftbtn;你确定你的代码没有问题吗。我认为问题在于你的形象名称。检查一下。图像名称很好。我使用下面的代码实现了输出。UIImage*img=[UIImage ImageName:@“Cross_Icon.png”];UIButton*btn=[UIButton按钮类型:UIButtonTypeCustom];btn.frame=CGRectMake(0,0,img.size.width,img.size.height);[btn setBackgroundImage:img for状态:uicontrol状态正常];[btn addTarget:self action:@selector(Cancelaction)for ControlEvents:UIControlEventTouchUpInside];uibarbuttoneim*lftbtn=[[uibarbuttoneim alloc]initWithCustomView:btn];self.navigationItem.leftBarButtonItem=lftbtn;但是任何人都可以解释为什么不能使用UIBarButtonItem*lftbtn=[[UIBarButtonItem alloc]initWithImage:img样式:uiBarbuttonItemStyleBordedTarget:self action:@selector(cancelaction)];self.navigationItem.leftBarButtonItem=lftbtn;你确定你的代码没有问题吗。我认为问题在于你的形象名称。检查一下。图像名称很好。我使用下面的代码实现了输出。UIImage*img=[UIImage ImageName:@“Cross_Icon.png”];UIButton*btn=[UIButton按钮类型:UIButtonTypeCustom];btn.frame=CGRectMake(0,0,img.size.width,img.size.height);[btn setBackgroundImage:img for状态:uicontrol状态正常];[btn addTarget:self action:@selector(Cancelaction)for ControlEvents:UIControlEventTouchUpInside];uibarbuttoneim*lftbtn=[[uibarbuttoneim alloc]initWithCustomView:btn];self.navigationItem.leftBarButtonItem=lftbtn;但是任何人都可以解释为什么不能使用UIBarButtonItem*lftbtn=[[UIBarButtonItem alloc]initWithImage:img样式:uiBarbuttonItemStyleBordedTarget:self action:@selector(cancelaction)];self.navigationItem.leftBarButtonItem=lftbtn;谢谢你的回复。但是我只使用当前视图控制器的导航项属性。这里我的当前视图控制器是EmployeeListTableViewController。我已将其指定为应用程序委托中UINavigationController的根视图控制器。感谢您的响应。但是我只使用当前视图控制器的导航项属性。这里我的当前视图控制器是EmployeeListTableViewController。我已将其指定为应用程序委托中UINavigationController的根视图控制器。感谢您的响应。但是我只使用当前视图控制器的导航项属性。这里我的当前视图控制器是EmployeeListTableViewController。我已将其指定为应用程序委托中UINavigationController的根视图控制器。感谢您的响应。但我使用的是当前的视图控件