UIPickerView BarButton 文本颜色没有改变,但是当点击按钮时,颜色在 Objective-C 中改变
Posted
技术标签:
【中文标题】UIPickerView BarButton 文本颜色没有改变,但是当点击按钮时,颜色在 Objective-C 中改变【英文标题】:UIPickerView BarButton Text Color is not change but when click on button then the color is change in Objective-C 【发布时间】:2019-01-25 05:41:36 【问题描述】:我有问题我已将 UIPickerView
BarButton
标题颜色设置为蓝色,但它不能提供完美的输出,但是当我单击该按钮时,它给出的输出意味着它会将颜色更改为蓝色,但我不想要仅在单击后为蓝色,我希望在单击前按钮颜色为蓝色。那我该怎么做呢?
代码:
-(void)SetToolbar
//******* add buttons in toolbar
self.toolbar=[[UIToolbar alloc]init];
self.toolbar.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin );
self.toolbar.frame=CGRectMake(0, self.currencypicker.frame.origin.y, self.currencypicker.frame.size.width, 45);
self.toolbar.barStyle=UIBarStyleDefault;
self.toolbar.backgroundColor=[UIColor whiteColor];
self.toolbar.tintColor=[UIColor blueColor];
UIBarButtonItem *donebutton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(Btn_DoneClick)];
// donebutton.style=UIBarButtonItemStylePlain;
UIBarButtonItem *cancelbutton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(Btn_CancelClick)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIView *titleview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
titleview.backgroundColor=[UIColor clearColor];
UILabel *lbltitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
lbltitle.backgroundColor=[UIColor clearColor];
lbltitle.textAlignment=NSTextAlignmentCenter;
lbltitle.text=@"Payment Type";
lbltitle.font=[UIFont boldSystemFontOfSize:17.0];
if(IS_IPAD)
lbltitle.textColor=[UIColor colorWithRed:113.0/256 green:120.0/256 blue:128.0/256 alpha:1.0];
else
lbltitle.textColor=[UIColor blackColor];
[titleview addSubview:lbltitle];
UIBarButtonItem *btnAdd=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPaymentClicked)];
UIBarButtonItem *titlebutton = [[UIBarButtonItem alloc] initWithCustomView:titleview];
NSArray *items = [NSArray arrayWithObjects:cancelbutton,flex,titlebutton,flex,btnAdd, donebutton, nil];
[self.toolbar setItems:items animated:NO];
[self.view addSubview:self.toolbar];
[self.view bringSubviewToFront:self.pickerView];
//*******
用于导航的我的代表 DidFinishCode:
[[UINavigationBar appearance] setBarTintColor:FooterBGColor];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
if (@available(ios 11.0, *))
[[UINavigationBar appearance] setLargeTitleTextAttributes:@NSForegroundColorAttributeName: [UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@NSForegroundColorAttributeName: [UIColor whiteColor]];
else
【问题讨论】:
我已经尝试过您的代码并正确显示“完成”按钮。某些东西必须从其他代码中破坏。 我在 AppDelegate 中编写了一些代码,我只是在这里更新代码,请检查一下,我不知道为什么会发生这种情况! 我也尝试过 appdelegate 代码,这也可以正常工作。必须与其他代码中断。 不确定,您可以试试这个***.com/a/47754627/2323806 & ***.com/a/47839657/2323806 太棒了:)快乐编码! 【参考方案1】:我知道这个问题是 10 个月前提出的。但是今天我仍然有这个问题。我尝试更改色调颜色,但我不知道为什么它不起作用。花了几个小时后,我终于决定在 UIToolbar 上创建一个 UIView,然后在上面添加一个 UIButton。它对我有用。我希望它可以帮助将来的人。
// UIPickerView
self.myPickerView = UIPickerView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
self.myPickerView.delegate = self
self.myPickerView.dataSource = self
self.myPickerView.backgroundColor = UIColor.white
cell.myTextField.inputView = self.myPickerView
// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// UIView
let myNewView=UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 44))
myNewView.backgroundColor = .lightGray
toolBar.addSubview(myNewView)
// UIButton
let button:UIButton = UIButton(frame: CGRect(x: myNewView.bounds.width - 80, y: 0, width: 70, height: 44))
button.setTitle("Done", for: .normal)
button.addTarget(self, action:#selector(self.doneClick), for: .touchUpInside)
myNewView.addSubview(button)
cell.myTextField.inputAccessoryView = toolBar
【讨论】:
【参考方案2】:在完成按钮中使用 tint Color 试试这个。
UIBarButtonItem *donebutton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(Btn_DoneClick)];
// donebutton.style=UIBarButtonItemStylePlain;
donebutton.tintColor = [UIColor blueColor];
【讨论】:
让 tool = UIToolbar();tool.barStyle = UIBarStyle.default tool.isTranslucent = true tool.tintColor = UIColor.blue tool.sizeToFit() tool.barTintColor = UIColor.blue tool.backgroundColor = UIColor.white // 这是 swift 代码,我希望你能在目标 c 中转换。 我尝试这段代码仍然无法正常工作@Pradip 我认为我们错过了一些东西!以上是关于UIPickerView BarButton 文本颜色没有改变,但是当点击按钮时,颜色在 Objective-C 中改变的主要内容,如果未能解决你的问题,请参考以下文章