I have using EKEventViewController in our app.
Code should like below
class MyEkEventViewController: EKEventViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(MyEkEventViewController.cancel))
self.navigationController?.navigationItem.setLeftBarButton(cancelButton, animated: false)
//self.navigationController?.navigationItem.leftBarButtonItem = cancelButton
}
}
#objc fileprivate func cancel() {
_ = navigationController?.popViewController(animated: true)
}
}
# My invocation
.
.
.
let eventViewController = MyEkEventViewController()
eventViewController.delegate = self
eventViewController.allowsEditing = true
eventViewController.allowsCalendarPreview = true
eventViewController.hidesBottomBarWhenPushed = true
eventViewController.event = event
self.navigationController?.pushViewController(eventViewController, animated: true)
Back arrow only displayed. But Cancel button is not displayed in left side navigation bar.
Related
I've a ViewController as popup to pick a date on UIDatePicker. But when, this popup opens, the background is covered by new background color. I tried choose a opacity of color, but this not works.
I use a UILabel to handler click and open a new ViewController.
override func viewDidLoad() {
super.viewDidLoad()
let tapDateSelectExpiration = UITapGestureRecognizer(target: self, action: #selector(handleSelectDateExpiration))
let tapDateSelectValidity = UITapGestureRecognizer(target: self, action: #selector(handleSelectDateValidity))
let tapDateSelectOrder = UITapGestureRecognizer(target: self, action: #selector(handleSelectDateOrder))
lblDataPedido.isUserInteractionEnabled = true
lblDataValidade.isUserInteractionEnabled = true
lblDataExpiracao.isUserInteractionEnabled = true
lblDataPedido.addGestureRecognizer(tapDateSelectOrder)
lblDataValidade.addGestureRecognizer(tapDateSelectValidity)
lblDataExpiracao.addGestureRecognizer(tapDateSelectExpiration)
}
#objc func handleSelectDateExpiration(_ sender : UILabel) {
print("handleSelectDateExpiration")
var vcSelectDate = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerSelectDate") as! ViewControllerSelectDate
vcSelectDate.typeDate = "DateExpiration"
self.present(vcSelectDate,animated: true,completion: nil)
}
// Other funcs as above.
MainLayout:
DatePicker layout with backgroud not transparent:
Storyboard:
You can try
let vc = self.storyboard?.instantiateViewController(withIdentifier: "dateView") as! dateViewController
vc.delegate = self
vc.providesPresentationContextTransitionStyle = true;
vc.definesPresentationContext = true;
vc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(vc, animated: false, completion: nil)
and select transparent background like this
or in IB
I have implemented a search controller in a viewController with a table view and also a search controller, the search bar is displayed when search button is taped, search button is a UIBarButtonItem and the searchBar is shown in the navigation controller but I want to make disappear when tap on cancel button, I have tried using serarchBarDelegate protocol but nothing happens on tap here the code
class NewsTVController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate{
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = true
self.searchController.hidesNavigationBarDuringPresentation = false
loadNews()
}
#IBAction func searchButtonTaped(sender:UIBarButtonItem){
print("tap inside")
self.navigationItem.titleView = searchController.searchBar
//self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
// searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){
//}
//self.navigationItem.rightBarButtonItem.
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
hideSearchBar()
}
func hideSearchBar() {
print("hay que escpder")
//navigationItem.setLeftBarButtonItem(searchBarButtonItem, animated: true)
//logoImageView.alpha = 0
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil// = self.logoImageView
self.navigationItem.title = "Eventos"
//self.logoImageView.alpha = 1
}, completion: { finished in
})
}
}
here the pic of the navigation controller with a search button and the search bar
what I want is that when the cancel button is tapped the navigation bar stay as in first pic, no search bar.
and some tips there is a way to hide the back button and the search button when search bar appears.
as i donĀ“t find a way to achive this i found this solution
#IBAction func searchButtonTaped(sender:UIBarButtonItem){
print("tap inside")
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.setShowsCancelButton(false, animated: false)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(hideSearchBar(sender:)))
//self.searchIcon.is
//self.navigationItem.rightBarButtonItem?.action = #selector(hideSearchBar(:))
// searchController.searchBar.touchesCancelled(UITouch, with: .touchUpInside){
//}
//self.navigationItem.rightBarButtonItem.
}
func hideSearchBar(sender:UIBarButtonItem) {
print("hay que escpder")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.search, target: self, action: #selector(searchButtonTaped(sender:)))
//logoImageView.alpha = 0
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil// = self.logoImageView
self.navigationItem.title = "Noticias"
//self.logoImageView.alpha = 1
}, completion: { finished in
})
}
and boila the search bar now dissapear from navigation controller nad the search button is functional since is linked to searchButtonTaped in Interface Builder
I am having trouble creating a UINavigationController toolbar programmatically. I have previously used storyboards to do this successfully but would like to try doing it all in code.
I have created the UIBarButtonItems programmatically, but the actual functions or actions they are supposed to perform when pressed are not working.
To clarify, I am NOT attempting to add UIBarButtonItems to the top bar. I've seen dozens of questions all asking the same thing. I am referring to the toolbar at the bottom that comes with the UINavigationController. This means no "rightBarButtonItem" or "leftBarButtonItem"
Here is the code:
class ProgOneViewController: UIViewController {
var chatRoomsButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Chats", style: .plain, target: self, action: #selector(segueToChatRoomController(_:)))
return button
}()
var exploreButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Explore", style: .plain, target: self, action: #selector(segueToExploreController(_:)))
return button
}()
var profileButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Profile", style: .plain, target: self, action: #selector(segueToProfileController(_:)))
return button
}()
// Flexible spaces that are added in between each button.
var flexibleSpace1: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace2: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace3: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace4: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
// These are the functions that are not being called for some mysterious reason.
func segueToChatRoomController(_ sender: Any) {
print("segueing to chat rooms controller")
let chatRoomsController = ChatRoomsViewController()
let navController = UINavigationController(rootViewController: chatRoomsController)
self.present(navController, animated: false, completion: nil)
}
func segueToExploreController(_ sender: Any) {
print("segueing to explore controller")
let exploreController = ExploreCollectionViewController()
let navController = UINavigationController(rootViewController: exploreController)
self.present(navController, animated: false, completion: nil)
}
func segueToProfileController(_ sender: Any) {
print("segueing to profile controller")
let profileController = ProfileTableViewController()
let navController = UINavigationController(rootViewController: profileController)
self.present(navController, animated: false, completion: nil)
}
func setUpToolbar() {
print("setting up toolbar")
self.navigationController?.setToolbarHidden(false, animated: false)
self.navigationController?.toolbar.isUserInteractionEnabled = true
let toolBarItems = [flexibleSpace1, chatRoomsButton, flexibleSpace2, exploreButton, flexibleSpace3, profileButton, flexibleSpace4]
self.setToolbarItems(toolBarItems, animated: true)
// For some reason, these two methods leave the toolbar empty.
//self.navigationController?.setToolbarItems(toolBarItems, animated: true)
//self.navigationController?.toolbar.items = toolBarItems
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
setUpToolbar()
}
}
EDIT*
Here is the code used to instantiate the ProgOneViewController inside a UINavigationController
func pushToTestProgrammaticallyCreatedViews() {
let progOneViewController = ProgOneViewController()
let navController = UINavigationController(rootViewController: progOneViewController)
//navController.isToolbarHidden = false
//progOneViewController.setUpToolbar()
self.present(navController, animated: false, completion: nil)
}
I called this function upon clicking a button in my storyboard-created view controller. The function signature was long in order to specify which viewControllers were tests (created programmatically) :)
first of all you have to make sure that your ProgOneViewController is really included in a UINavigationController. if that is the case the toolbar as well as the barbuttonitems are shown (tried your code in my project).
except of that you have to change all of your barbuttonitem declarations to lazy var so that the references to self within the declarations point to the viewcontroller and target-action works.
feel free to ask if there are any questions :)
I have created
NavigationController(Main) - > LoginViewController -> Tabbarviewcontroller -> HomeViewController
If I add barbutton item in HomeViewController through storyboard it's not displaying in simulator.
But I can see the changes in storyboard.
Title Home1 and barbutton item not displaying in simulator
Try this Code: Tested in Swift 3:
Note: Delete all your barButtonItems and try below code.
Add this code to your Home1 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Add Button pressed")
}
Add this code to your Home2 VC:
override func viewWillAppear(_ animated: Bool) {
let RightButtomitem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = RightButtomitem
}
func handler(sender:UIButton) {
print("Done Button Pressed")
}
Output:
First you hide Navigation Bar in NavagationController and you make custome bar After you Add in it which you want in bar .
You can try it programmatically
HomeViewController ---
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
let barButtomitem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(handler))
self.tabBarController?.navigationItem.rightBarButtonItem = barButtomitem
}
SecondItemViewController--
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
self.tabBarController?.navigationItem.rightBarButtonItem = nil
}
and don't add barbutton in storyboard ...
I have 3 pages
Page 1: Menu
Page 2: Menu > Navigation Controller > Map listview
Page 3: Menu > Navigation Controller > Map
It's possible to switch between page 2 and 3 but when you click "Back" it always goes to page 1 and I did this using a custom back button.
After having used the custom back button once the following problem appears:
When I go to page 2 or 3 from the Menu page (Page 1) the navigation title appears and in less than a second it disappears. How is this possible?
These are the functions I am using:
private func hideAndAddNewBackButton(){
if backToRoot{
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = newBackButton;
self.title = "Locaties"
}
}
func back(sender: UIBarButtonItem) {
if let viewController2 = storyboard!.instantiateViewControllerWithIdentifier("ViewController2") as? ViewController2{
self.navigationController?.pushViewController(viewController2, animated: true);
}
}
func needBackToRoot(){
backToRoot = true;
}
And this is in my viewDidLoad():
var backToRoot:Bool = false;
override func viewDidLoad() {
super.viewDidLoad()
self.hideAndAddNewBackButton();
}
My switch button:
#IBAction func showLijst(sender: AnyObject) {
if let viewController3 = storyboard!.instantiateViewControllerWithIdentifier("Lijst") as? KaartListview{
viewController3.needBackToRoot();
self.navigationController?.pushViewController(viewController3, animated: true);
}
}
In my case problem was creating custom back button and setting in pushed controller
self.navigationController?.navigationBar.topItem?.title = ""
My solutions is :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.topItem?.title = "SomeTitle"
}
I had a similar issue before and I fixed by using:
navigationController?.navigationBarHidden = false
In the viewDidLoad() function
Like this:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBarHidden = false
}