Change color of UIBarButtonSystemItemCancel

大兔子大兔子 提交于 2019-12-13 05:44:43

问题


If I use the UIBarButtonSystemItemCancel, it's color is set to that of the navigation bar. However, in the Photos App, the "Cancel" button is shown with a blue background. How can I also set the UIBarButtonSystemItemCancel to have a blue background please?


回答1:


your can use image as button image as

UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];


//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

[button addTarget:self action:@selector(clickActionItem:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button ];




self.navigationItem.rightBarButtonItem = rightBarButtonItem;



回答2:


set the theNavigationController.navigationBar.barStyle to UIBarStyleBlackTranslucent

theNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

and the buttons will be blue by default.




回答3:


There is a better way than using an image. The UISegmentedControl can be configured to look just like a UIBarButtonItem and UISegmentedControl has a tintColor property.

UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
    button.momentary = YES;
    button.segmentedControlStyle = UISegmentedControlStyleBar;
    button.tintColor = color;
    [button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

I built a UIBarButtonItem category that'll do this.



来源:https://stackoverflow.com/questions/4919614/change-color-of-uibarbuttonsystemitemcancel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!