On the left side of a navigation controller I have a button I moved in through the storyboard. I have no issues configuring that one of course, but I wanted to use the Edit button you get from uncommenting out:
self.navigationItem.rightBarButtonItem = self.editButtonItem()
But how can I customize the font name, size, and color on this button?
Here's what I tried after looking around:
let attributes = [NSFontAttributeName:UIFont(name: "HelveticaNeue-Thin", size: 19)!]
let fontColor = [NSStrokeColorAttributeName:UIColor.whiteColor()]
self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, forState: .Normal)
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(fontColor, forState: .Normal)
Neither of these attributes work. Is there either:
Another way of customizing the edit button you get from that viewDidLoad line (since there's no way to do it from Storyboard) or
Is there a way to reproduce what the edit button does in code so i can customize it in Storyboard?
Thanks for any help.
There is a way to customize the edit button you get from that viewDidLoad line. You were close, I just combined the attributes together and used NSForegroundColorAttributeName instead of NSStrokeColorAttributeName
let attributes = [NSFontAttributeName:UIFont(name: "HelveticaNeue-Thin", size: 19)!, NSForegroundColorAttributeName:UIColor.whiteColor()]
self.editButtonItem().setTitleTextAttributes(attributes, forState: .Normal)
self.navigationItem.rightBarButtonItem = self.editButtonItem()
Related
Is there a way we can customize the code below on a nav bar which is from the object library and not a nav bar which is embedded in? I have the code below. It's working on a nav bar that is embedded on my view but when I create my own nav bar from the object library, the code below is no longer working.
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Chalet-NewYorkNineteenEighty", size: 37.0)!];
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
For the most part I assume you are creating UINavigationBar in xib, in that case IBOutlet wont be UINavigationBarController, give this a try
navigationBar.tintColor = UIColor.white
navigationBar will be outlet var name...
If this doesn't work more info might help...
I changed back button text with below code, here domain is the string of a url which may be a very long string. So how to display it correctly when it is very long?
let backButton = UIBarButtonItem(title: domain, style: .plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButton.
There is a limit set to the back button title. This limit cannot be determined through the Apple doc. If you try to set the longer title than specified limit Apple has then this longer title will be replaced with "Back" title. And it hardly makes sense to have the back button title that big, it really hampers the user experience.
Just do not use a long title, look how Apple does it, when title is too long then they use a generic back title. You will not get a good result from a long back button title no matter how you implement it...
let barButton = UIButton()
barButton.frame = CGRect(x:0, y:0, width:30, height:30)
barButton.setTitle(“BarButtonTest”, for: .normal)
barButton.backgroundColor = UIColor.yellow
barButton.layer.cornerRadius = 5.0
barButton.addTarget(self, action: #selector(didTapBarButton(_:)), for: .touchUpInside)
let rightBarButton = UIBarButtonItem(customView: barButton)
self.navigationItem.rightBarButtonItem = rightBarButton
If above answer not solve your problem then instead of using navigation bar use custom view as Navigation Bar using autolayout and take button inside it, set your title accordingly.
I want to change all the back arrows in all navigation controllers throughout my app. I want to avoid changing the image manually in every file as that would be time-consuming. I'll post the code I have so far in my AppDelegate file in the didFinishLaunchingWithOptions method.
let backButton = UIImage(named: "backbutton-thin")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 16, 16))
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 5, topCapHeight: 0)
barButtonAppearance.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
let item = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationController.self])
item.setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
item.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
The code produces this result in all my navigation bars ...
As you can see, the thin arrow is overlapping the "Back" text and the original arrow is still there. What am I doing wrong? How can I remove the original arrow and replace it with my thin custom arrow? I still want to keep the text though. Any help would be appreciated.
You need to modify the backIndicatorImage and backIndicatorTransitionMaskImage of the UINavigationBar class to replace the existing back indicator image with your custom one.
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "backbutton-thin")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "backbutton-thin")
I want to have the back button with "< [IMG]" instead of "< Back" which is by default.
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
with this I have removed the text but how to add an image next to the "<".
let backButton = UIBarButtonItem(image:UIImage(named: "ic_authors_detail_back.png"), style:.Plain, target:self, action:"backButtonPressed");
backButton.tintColor = UIColor(hexString: "#ffffffff")
let leftBarButtonsArray = [backButton]
navigationItem.leftBarButtonItems = leftBarButtonsArray
Here is the code to add a button on navigation bar.Hope this helps
When you click left bar button
You can set its attribute like:
Here is option to set image.
I recently migrated from Swift 2 to Swift 3 and I'm stuck on one part. In my AppDelegate, I'd set up the default settings for the UINavigationBar as coded below. The challenge I'm having is that the setBackgroundImage is no longer recognized in Swift 3 and I can't find an alternative.
Has anyone had the same issue happen and been able to resolve it?
let turquoiseBackgroundImage:UIImage = UIImage(named: "navigationBarTurqoise")!
**// TODO Below is not working with Swift 3
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)**
// Set up Back button to be white in Navigation bar
UINavigationBar.appearance().barStyle = UIBarStyle.default
UINavigationBar.appearance().tintColor = UIColor.white
// Font Name and Size of Title in Navigation Bar
if let font = UIFont(name: "TitilliumWeb-Light", size: 20) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor.white]
}
// Remove hairline between navigation bar and anything below such as search bar
UINavigationBar.appearance().shadowImage = UIImage()
// Set up status bar (battery, time, etc.) to white
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)
rewrite code like this
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, for: .default)