If you are familiar with UIImagePickerController screen for .PhotoLibrary, on the right hand side there is a Cancel button. On the left hand side, I want to add a Camera icon that can be found in XCode library.
My code doesn't work.
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
picker.allowsEditing = true
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: picker, action: Selector("btnOpenCamera"))
picker.navigationItem.leftBarButtonItem = camera
presentViewController(picker, animated: true, completion: nil)
Referred to this post, and interpret from objective C to Swift.
I found a solution to my question. This method will do whatever customisation I need inside a loaded view controller.
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
viewController.navigationItem.leftBarButtonItem = camera
}
It may reset navigationItem to other somewhere else during code execution (for example in viewDidLayoutSubviews).
You can try to inherit from UIImagePickerController and reimplement
func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: , action:self Selector("btnOpenCamera"))//you can handle this with delegate if you want
self.navigationItem.leftBarButtonItem = camera
}
Than just use your inherited class instead of standard UIImagePickerController. Hope it helped.
Related
I was having this issue and I've tried a lot of solutions that was proposed by some kind people here in the following topic:
Swift - How to hide back button in navigation item
I created a ViewController class:
import SwiftUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
}
and AS you can see in the above code I tried every single way with no change - back button still appear - then I try to make simple change like change the text of the back button or the shape and also there is no result!!
Am I do something wrong :( Because I feel like the whole class is not active for my view
Do I need to create an object of ViewController or something like that? Because I just wrote the mentioned code about my view code.
MY GOAL: I just want to move from view to another with no back button if there is another way I wouldn't mind to do it.
PPLLLSSSS HELPP ME Guys I'm so tired, I'll work on another things until find a solution for that and I'm sure there is a lot of people who want a solution for that issue.
Once I find the solution I'll share it with you guys :) Best Wishes and Regards
You just have to add the below code in the ViewController where you want to hide the backbutton.
navigationItem.setHidesBackButton(true, animated: true)
Segue from viewController to viewController2 and name the segue testSegue. This should work.
I had the exact same problem and the only solution that worked was adding
.navigationBarBackButtonHidden(true) in my SwiftUI view
I am trying to make a custom back button using this code:
let back = UIImage(named: "header_backarrow")
let backView = UIImageView(image: back)
let backItem = UIBarButtonItem(customView: backView)
navigationItem.leftBarButtonItem = backItem
I want the navigation item to perform this code:
func dismissManual() {
dismiss(animated: true, completion: nil)
}
I have tried many things like following this Stack Overflow post: Execute action when back bar button of UINavigationController is pressed
I have also tried making it a navigationItem.backBarButtonItem; however, nothing seems to work. Some things show the correct custom image, but do not work as a button; on the other hand, some work as a button, but do not show the correct image.
Does anybody know how I can show the correct image and make the item work as a button? Thanks.
Do it as follows:
override func viewDidLoad() {
super.viewDidLoad()
let back = UIImage(named: "header_backarrow")
let backView = UIImageView(image: back)
backView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissManual))
backView.addGestureRecognizer(tap)
let backItem = UIBarButtonItem(customView: backView)
navigationItem.leftBarButtonItem = backItem
}
#objc func dismissManual() {
print("print----")
// dismiss(animated: true, completion: nil)
}
Add gesture to backView it will work!
It's similiar to this question IOS - Swift - adding target and action to BarButtonItem's customView
Swift 4.1
The issue is that UIImage does not have tap recognition. You will have to add a tap gesture recognizer to your backView.
lazy var singleTap: UITapGestureRecognizer = {
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
singleTap.numberOfTapsRequired = 1
return singleTap
}()
// Actions
#objc func tapDetected() {
dismiss(animated: true, completion: nil)
}
If you show your code, with some screenshots I can give more help if this doesn't solve the issue.
You are creating your UIBarButtonItem incorrectly. You do not need the image view.
Do it as follows:
let back = UIImage(named: "header_backarrow")
let backItem = UIBarButtonItem(image: back, style: .plain, target: self, action: #selector(dismissManual))
navigationItem.leftBarButtonItem = backItem
#objc func dismissManual() {
dismiss(animated: true, completion: nil)
}
Note that the function must be marked with #objc.
Depending on your image and how you want it displayed, you may need to create the image as follows:
let back = UIImage(named: "header_backarrow").withRenderingMode(.alwaysOriginal)
Another option is to create a UIButton with the image and setup to call your dismissManual function. Create the UIBarButtonItem with the button as the custom view.
But it's easier to create a standard UIBarButtonItem when all you have is a simple image or a simple string.
let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
style: .plain,
target: self,
action: #selector(menuButtonTapped))
// Adding button to navigation bar (rightBarButtonItem or leftBarButtonItem)
self.navigationItem.rightBarButtonItem = barButtonItem
// Private action
#objc fileprivate func menuButtonTapped() { // body method here }
Check out this, it may help Thanks.
As per title, my Camera system icon comes in and goes off almost immediately. However, when I try to navigate to another screen, it stays there permanently.
Video of how it disappears.
https://vid.me/ggk5
My codes are as below. I've set my view controller as a delegate of UINavigationControllerDelegate. So when the imagepickercontroller is shown, I want to add a camera icon as per the video. However it comes in and goes off.
public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool){
let camera = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(CameraTabTVC.btnOpenCamera))
viewController.navigationItem.leftBarButtonItem = camera
}
func btnOpenCamera(){
self.dismiss(animated: false, completion: nil)
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
picker.allowsEditing = true
present(picker, animated: false, completion: nil)
}
I'm trying to connect an action to my UIBarButtonItem in Swift, programmatically, without any Storyboard.
I can't do it this way :
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: "sayHello")
Since I use an external library (Font_Awesome_Swift) which doesn't have a constructor to create an UIBarButtonItem with an icon from the library. So I'm doing it this way :
let rightButtonItem = UIBarButtonItem()
rightButtonItem.FAIcon = FAType.FACamera
Then, I want to be able to attach an action to this UIBarButtonItem. I have found in another answer, this solution :
UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: self, forEvent: nil)
But I don't really understand how to use it. Where should I indicate the name of the selector ?
Thanks!
Swift makes adding an action to a UIBarButtonItem easy for us.
let rightBarButton = UIBarButtonItem()
// add an action
rightBarButton.action = #selector(yourAction)
Ok, just after posting this question, I have found an easy way to solve my issue... With the "default" UIBarButtonItem constructor and the following code :
let rightButtonItem = UIBarButtonItem(title: "Camera", style: .Plain, target: self, action: "launchCamera")
rightButtonItem.FAIcon = FAType.Camera
for example i write this code for UIBarButtonItem:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action , target: self, action: #selector(shareTapped))
and selector code is :
func shareTapped() {
// for share in Social media or Save in local
// let vc = UIActivityViewController(activityItems: [imageView.image!], applicationActivities: [])
// vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
// present(vc, animated: true)
// this line its for share photo in facebook or twitter
if let vc = SLComposeViewController(forServiceType: SLServiceTypeTwitter) {
vc.setInitialText("Look at this great picture!")
vc.add(imageView.image!)
vc.add(URL(string: "http://www.photolib.noaa.gov/nssl"))
present(vc, animated: true)
}
}
and i use image in my app and because of this i using image view.image
I want to add a Camera option on my left bar item inside .PhotoLibrary My codes as shown below, and they don't work.
let picker = UIImagePickerController()
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
picker.navigationItem.leftBarButtonItem = camera
picker.allowsEditing = true
picker.sourceType = .PhotoLibrary
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
You cannot do this the way you are doing because there is no UINavigationController to UIImagePickerController. In order to do this you must add UIInavigationControllerDlegate method in your class.
Add this method in your viewController
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
print("m in")
viewController.navigationItem.title = "Home"
let camera = UIBarButtonItem(title: "camera", style: .Plain, target:self, action: "btnOpenCamera")
viewController.navigationItem.leftBarButtonItem = camera
The output is like this:
Referred to this post, and interpret from objective C to Swift.
I found a solution to my question. This method will do whatever customisation I need inside a loaded view controller.
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
viewController.navigationItem.leftBarButtonItem = camera
}