image and text inside UITabBarItem - ios

I'm trying to add a text and an image inside a UITabbarItem but it didn't work when I used UITabBarItem inside the Navigation Bar.
So, I have added UIButton inside the Navigation Bar and set image and title for it like this:
Click to see example image
Here is what I did :
let button = UIButton(type: .system)
button.setImage(UIImage(named: "sortIc"), for: .normal)
button.setTitle("ترتيب",for: .normal)
button.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
What I want to do is to change the place of the image to the text place and vice versa. Any suggestions ?

Related

Using a UIButton into Navigation Bar using StoryBoard

Essentially I have a UIButton created in StoryBoards that I would like to drag into the navigation bar. This UIButton has an action associated with it when touched but all touch events stop working when I drag this UIButton into the navigation bar.
Is there additional code I need to the IBAction item when doing this?
One of the main reasons I want to add a UIButton instead of a BarButtonItem to the navigation bar is simply because it allows me to add a background color and curve the edges of the button.
I don't think you can do it in the storyboard. You have to do it in code, using the init(customView:) initialiser.
let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...
// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items
...
#objc func buttonAction() {
...
}

How to set custom navigation controller back arrow image throughout app in the app delegate file?

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")

NavigationItem not showing on iPhone 5S/5C size

I have the following snippet to add a "next" button to the top right of my nav bar.
let next = UIButton()
next.setTitle("Next", for: .normal)
next.addTarget(self, action: #selector(nextButtonHandler), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: next)
This is showing fine on regular size phone, but NOT on my 5C... I can't figure out why.. I put a breakpoint where the right bar button item is set and it is NOT nil.. Help!
Use this:
let barButton = UIBarButtonItem(title: , style: , target: , action: )
Add the button to the navigationController
super easy

Setting leftBarButtonItem not working

I haver a ViewController in which I'm trying to override the default back button with a custom one. Here is my code:
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
myBackButton.setTitle("Back Plz", for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
This is not working (I'm still seeing the default back button in the navigation bar). I also tried setting 'backBarButtonItem' and setting 'hidesBackButton = true' and neither worked.
Not sure if it's relevant but this ViewController is being "Push"ed from an embedded View Controller, so my general Storyboard setup is this:
Navigation Controller -> View Controller -> Embedded View Controller -> View Controller (where I'm working)
What am I doing wrong? Thanks!

Navigation Left Bar Button Item as Image Logo

I'm trying to add an image to a left on a Navigation Bar using leftBarButtonItem that will serve as a small logo. I'm able to add the image perfectly fine. My question is how do I disable it from being clickable and just keep the same state so it doesn't turn gray when someone presses it. I just want it as an image in the left of the Navigation Bar. Below is what I use to add the logo to the left of the navigation bar. Any help is greatly appreciated.
let btnName = UIButton()
btnName.setImage(UIImage(named: "NavIcon"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 40, 50)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
btnName.userInteractionEnabled = false

Resources