Custom Uibarbuttonitem with Bg Colour and Text Swift 3

Custom UIBarButtonItem With BG Colour and Text Swift 3

For that you have only one option you need to create UIButton instance with design that you want after that create UIBarButtonItem instance from that button.

let btnProfile = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnProfile.setTitle("SY", for: .normal)
btnProfile.backgroundColor = UIColor.yellow
btnProfile.layer.cornerRadius = 4.0
btnProfile.layer.masksToBounds = true

Now create the UIBarButtonItem from that button and set it with the rightBarButtonItems.

navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), UIBarButtonItem(customView: btnProfile)]

Set UIBarButtonItem's background color working with Storyboard swift

First, drag and drop a UIButton to UINavigationBar. Then from Document Outline select the UIButton which is a child of UIBarButtonItem.

Sample Image

Now go to Identity Inspector from the right panel. Here in User Defined Runtime Attributes section you can set backgroundColor, cornerRadius etc. of UIButton.

Sample Image

But the modification does not reflect in Storyboard. Run your project and you can see the changes.

Sample Image

How can I change the background color of a UIBarButtonItem on iOS 7+?

Create a UIButton and use it as the custom view for the UIBarButtonItem. Then, set the backgroundColor on the button's layer:

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Test"];
button.layer.backgroundColor = [UIColor redColor].CGColor;
button.layer.cornerRadius = 4.0;

UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.toolbarItems = @[buttonItem];

Changing font color of UIBarButtonItem

What about using:

func setTitleColor(UIColor?, for: UIControlState)

Documentation says it sets the color of the title to use for the specified state.

sortButton.setTitleColor( .red, for: .normal)

Change the background color of a UIBarButtonItem in ios 7

Use appearance proxy for this,

Create a 1x1 pixel image with the color you prefer. In this case this image's name is "icons_gb.png". Then add following code to your AppDelegate.m . Image color will be repeated in the button's background.

UIImage *btnBg = [[UIImage imageNamed:@"icons_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UIBarButtonItem appearance] setBackgroundImage:btnBg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];

UIBarButtonItem with color?

If anyone is looking for code to exactly duplicate a simple UIBarButtonItem:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];

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

And delete.png is:

delete.png

How to change tintColor of UIBarButtonItem in Swift?

The problem was that the button was automatically set as custom. I refigured it to system.

How to apply borders and corner radius to UIBarButtonItem?

If you can find the UIView associated with the UIBarButtonItem, you can modify the UIView.layer. But, finding the UIView is not made easy. I used the technique from Figure out UIBarButtonItem frame in window? . Starting with the navigationController.navigationBar, which itself is a UIView, I recursed through the subviews, one of which will be the button I wanted. Which one? Pick your own criteria! Here's my code:

func recurseViews(view:UIView) {
print("recurseViews: \(view)") // helpful for sorting out which view is which
if view.frame.origin.x > 700 { // find _my_ button
view.layer.cornerRadius = 5
view.layer.borderColor = UIColor.redColor().CGColor
view.layer.borderWidth = 2
}
for v in view.subviews { recurseViews(v) }
}

then in viewDidAppear (or similar)

recurseViews((navigationController?.navigationBar)!)

which is a bit of a hack but does the job:

button with border

Globally change UIBarButtonItem text color when selected or highlighted

You have your didFinishLaunchingWithOptions function in your AppDelegate witch tells the delegate that the launch process is almost done and the app is almost ready to run.

And in there you can use the appearance on your UIBarButtonItem and UINavigationBar.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Text
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .selected)

// Images
UINavigationBar.appearance().tintColor = UIColor.orange

return true
}

Result:
Sample Image



Related Topics



Leave a reply



Submit