gpt4 book ai didi

ios - 在 ios 中为 UINavigationitem(Leftbarbutton 项目)设置图像

转载 作者:行者123 更新时间:2023-11-28 20:01:51 25 4
gpt4 key购买 nike

我正在尝试使用以下代码为 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];

提前谢谢你......

最佳答案

 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];
}

关于ios - 在 ios 中为 UINavigationitem(Leftbarbutton 项目)设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23668709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com