swift xcode change size for navigationItem.leftBarButtonItem = UIBarButtonItem - swift

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.

Related

Swift - Popover controller shows up on wrong corner depending on load time

I recently started having an issue where the popover controller will some times show up on the wrong corner. When it works correctly, the popover source is a button on the upper right corner. This is what happens most o the time. Now, when I land on this page "too quickly" from a specific route, the popover shows up on the upper left corner.
Another thing that is weird is that when loading is artificially delayed (like when I set break points for debugging) the popover shows up correctly.
Since it seemed to be a timing issue, I tried moving the navigation configuration from viewDidLoad to viewDidAppear, but this didn't work.
This function is called from viewDidLoad:
func configureNav() {
if canAddDoc == true {
let customButton = UIButton(type: .system)
customButton.setImage(with: .add, tintColor: UIColor.white, imageEdgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
customButton.addTarget(self, action: #selector(showAddDocSheet), for: .touchUpInside)
addButton = UIBarButtonItem(customView: customButton)
}
shareButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.action,
target: self,
action: #selector(shareButtonPressed))
navigationItem.rightBarButtonItems = (addButton == nil) ? [shareButton!] : [shareButton!, addButton!]
#objc func showAddDocSheet() {
guard let addButton = addButton else { return }
self.displayActionSheet(sourceButton: addButton)
}
}
func displayActionSheet(sourceButton: UIBarButtonItem) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
//Action sheet code goes here
if let popoverController = actionSheet.popoverPresentationController {
popoverController.barButtonItem = sourceButton
popoverController.sourceView = sourceButton.customView
popoverController.permittedArrowDirections = .up
}
self.present(actionSheet, animated: true, completion: nil)
}
This may sound a weird solution but something similar happened to me before and it got solved when setting the animation to false while presenting the VC ,
self.present(actionSheet, animated: false, completion: nil)

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: 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.

How can I set image location correctly in 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

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.