How can I set image location correctly in Swift? - swift

In my app, I am using SWRevealViewController. Currently everything is working OK except for the menu image position.I set up the menu button programatically. Now I need to put it to the correct place.It also needs to work in all screen sizes and needs to be at the same place.
I set up the menu button like this
override func viewDidAppear(animated: Bool) {
let image = UIImage(named: "reveal-icon#2x.png")
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style:
UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem?.image = image
}
and here is the result
How do I fix it? I think I need to do it programatically...
And here is the overall layout of my storyboard file.
Thanks for your answers.

This might be happening because you are initializing the UIBarButtonItem with a title. Initialize it with something like this:
let img = UIImage(named: "reveal-icon#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem = leftBarButtonItem

Related

UIKit: Adding text to NavigationBar

I'm diving into UIKit and feel some misunderstanding after using SwiftUI. By following course from Paul Hudson there is some challenges in the end of the project. One of this it's try showing the player’s score in the navigation bar. I try to solve this case but closest solution that I found from Apple's documentation it's:
let scoreButton = UIBarButtonItem(title: NSLocalizedString("Score: \(score)", comment: ""),
style: .done,
target: self,
action: nil)
navigationItem.rightBarButtonItem = scoreButton
But I think adding Button instead of simple Text is unnecessary, I don't need button behavior. Trying to add UITextView() to NavigationBar from storyboard did't work out...
Question: how to add text(better programmatically) to Navigation Bar? something like .toolbarItem with ToolbarItemPlacement: .navigationBarTrailing in SwiftUI. Is it possible or it's a UIKit restriction using only rightBarButtonItem?
You can use titleView property to show a text in the NavigationBar
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
view.addSubview(titleLabel)
view.addSubview(scoreLabel)
navigationItem.titleView = view
}
I found solution how to add text to NavBar and yes it's restriction of UIKit thats you have to use UIBarButtonItem
let scoreView = UILabel()
scoreView.text = "Score: \(score)"
let scoreButton = UIBarButtonItem(customView: scoreView)
navigationItem.rightBarButtonItem = scoreButton

How to set navigationBar back button image to default symbols?

there is one screen that i don't want to show back button symbols.
i create a empty image and change the navigation bar back button image.(code like following)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "mainicon_gray-13")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "mainicon_gray-13")
navigationItem.backBarButtonItem = UIBarButtonItem(title: "demo", style: .plain, target: nil, action: nil)
screen like following Picture
But all of backBarButtonItem changed, i need to set backBarButtonItem to default symbols "<" when i back to the view.
Is there any way to set navigation bar back button image to default image?
i want the navigationBar like following picture
following is the way that i found without change back button settings to do same event.
use leftBarButtonItem and set popViewController to go back before screen.
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "test", style: .plain, target: self,action: #selector(backViewBtnFnc))
}
#objc func backViewBtnFnc(){
self.navigationController?.popViewController(animated: true)
}

swift xcode change size for navigationItem.leftBarButtonItem = UIBarButtonItem

wanting to set the size for the image that show in the simulator as it comes up far to big i've tried many thing to no avail ?
func CancelButton(){
navigationItem.leftBarButtonItem = UIBarButtonItem(image:
#imageLiteral(resourceName: "HOME BUTTON BLACK").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleCancel))
}
func handleCancel() {
self.dismiss(animated: true, completion: nil)
}// handleCancel
You need a smaller image!
You can either do this in code, or replace the current image with a smaller one.
For bar button items, the best size is around 25x25, so try to resize it to that.
To do it in code, you can refer to this answer.

Swift: Custom Nav Bar Image not firing action on click

I have a navigation bar on my viewcontroller.
I have created a custom image (of a cog) and have got that to show right:
//Add bar item
var image = UIImage(named: "settingsIcon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
NavigationBar.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: "addTapped")
I added the action to the above code:
action: "addTapped"
Then I created a function:
func addTapped() {
print("Tapped")
}
When I run the app, the image shows fine. When I click the image nothing happens, not even an error.
Am I doing something blatantly wrong here?
Try setting the target to self instead of nil.

Swift UIToolbar positioning

I have a UIToolbar, but I have no idea how to positioning it..
I would add UIBarButtonItems to it, and positioning them. The toolbar showing up, but i can't change it size, and i can't position the buttons in it.
override func viewDidAppear(animated: Bool) {
self.navigationController?.setToolbarHidden(false, animated: true)
self.navigationController?.toolbar.frame = CGRectMake(50, 50, 50, 50)
self.navigationController?.toolbar.sizeToFit()
let plusImg = UIImage(named: "navbar_icon-02.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let plusButton = UIBarButtonItem(image: plusImg, style: UIBarButtonItemStyle.Plain, target: self, action: "postLeft:")
let filterButton = UIBarButtonItem(title: "Feed filter", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
let leftButton = UIBarButtonItem(title: "Button 3 tst", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
var toolbarButtons = [filterButton]
toolbarButtons.append(plusButton)
toolbarButtons.append(leftButton)
self.toolbarItems = toolbarButtons
}
There are two possible problems you might be having (I can't quite tell, from the way you've asked the question).
One problem might be that you are not in a navigation interface - you simply have a "loose" toolbar. Thus, the toolbar referred to through self.navigationController? is not your toolbar, and setting self.toolbarItems has no effect - those things are only for when you are in a navigation interface and the toolbar in question is the navigation controller's toolbar.
The other problem might be that you are in a navigation interface, in which case the toolbar position is not up to you - it's up to the navigation controller, which will always place it at the bottom of the screen.
So, either you need to be in a navigation interface so that the toolbar is the navigation controller's toolbar (and then your code will work, except for the positioning part), or else you can use a "loose" tolbar, in which case you need your code to refer to your toolbar, in which case you are free to position it, and you can give it items by setting its items.