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
.
Now go to Identity Inspector from the right panel. Here in User Defined Runtime Attributes section you can set backgroundColor
, cornerRadius
etc. of UIButton
.
But the modification does not reflect in Storyboard. Run your project and you can see the changes.
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:
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:
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:
Related Topics
Swift 3: Transfer Utility Enumeratetoassignblocks Method Signature
Background Image Not Displaying Correctly in iOS
How to Connect Xcode 9 and Github
Save The Exif Metadata Using The New PHPhotolibrary
How to Use Third Party Lib in Embedded Dynamic Framework for iOS with Swift
Start Position of Table View Is Wrong in Simulator, and Is Not The Same in Storyboard
How to Create Custom Album in Swift 3 iOS 10
iOS - Emphasise with Bold Strings in Localizable.Strings
How to Launch IPA File in iOS 10 Real Device Using Appium 1.6.0
How to Compile Aws Customidentityprovider on Xcode 8 Beta 6
Revealviewcontroller() Always Returns Nil
Responseserializer 'Cannot Call Value of Non-Function Type 'Nshttpurlresponse'' with Swift 3
App Crashes After Updating Coredata Model That Is Being Displayed in a UItableview
Uicollectionviewcompositionallayout - Center Items in Sections or Groups