Navigation bar buttons set during viewDidLoad don't appear until after view has appeared in iOS 11 - ios

In iOS 10, I could determine the list of navigation bar buttons I want to appear in viewDidLoad, and they would appear in the navigation bar as the view controller transitioned in.
In iOS 11, I can no longer do this. Whatever navigation bar buttons were set in interface builder are displayed as the view controller transitions in, and my custom list of buttons are not displayed until after the view finishes sliding in. Updating the buttons in viewWillAppear does not help.
Is this simply a bug in iOS 11, or is there something else I should be doing? Is there a workaround so I can continue to display buttons while the screen loads?
In the following example, I have set a button "Default Button" in the storyboard, and override it with an "Edit" button in viewDidLoad. The project is available on Github.
iOS 10
iOS 11

It looks like the issue is that navigation bar icons displayed during the transition appear to be fixed when the view controller is passed off to the navigation controller. By the time that viewDidLoad is called, the icons have already been fixed.
In order to fix this, we need to update the navigation bar icons on the view controller's navigationItem before the view controller is pushed onto the navigation controller stack.
One way to do this would be to setup the navigation bar icons in awakeFromNib. This is what #Joe's answer was effectively doing, because apparently viewDidLoad is called from awakeFromNib when isViewLoaded is true during awakeFromNib.
However, doing this in awakeFromNib prevents you from taking into account any properties set on the view controller in prepareForSegue. So another option (and the one that I am using) is to force the entire view to load in prepareForSegue by adding the line _ = controller.view after setting any desired properties.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
...
_ = controller.view
}
}
}

Move your Edit barButton code from viewDidLoad to isViewLoaded method as below.
override var isViewLoaded: Bool {
self.navigationItem.rightBarButtonItem = self.editButtonItem
return true
}
Output:
Note: Above code will fix the transition delay issue. Really, don't have much to explain why this happening. I experienced similar issue in iOS 10 as well NavigationBar delay updating barTintColor iOS10. It could be another bug in iOS11.
PS:
After reading Apple Doc about isViewLoaded and comparing other view loading methods. Its all about loading memory upon view loads.
You don't really need to move you barButton code at all.Just implement the isViewLoaded method and return to true as below:
override var isViewLoaded: Bool { return true}

Related

Two NavigationBar Showing

Hi I am new to Swift and am trying to build an app with multiple Views..
My first View(initial view) is embedded in navigation controller.
My Second View is embedded in Tab Bar Controller
My Third View is again embedded in a Navigation Controller.
The problem is that on my third view I see to navigation Controller with the top one taking me back to First View and the below one taking me to Second View.
Is it an incorrect way of doing this? I want to get rid of navigation bar that came from 1st view.
Thanks in anticipation.
PS : I had initially not attempted Navigation Bar on 3rd View.. but the problem was that I am also not able to map Bar Button Item and hence to embed the 3rd View too in a separate Navigation Controller
While it shows perfect in Xcode.. it shows 2 NavBar on the simulator
Not an elegant solution but still this can solve your problem. On your controller embed to UITabBarController where you have added Next Button. Add the below code on that controller class.
On ViewWillappear add show nav bar and on viewDidDisappear hide nav bar as shown in below code
ON viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
self.navigationController.navigationBar.isHidden = false
}
ON viewDidDisappear:
override func viewDidDisappear(_ animated: Bool) {
self.navigationController.navigationBar.isHidden = true
}

Why viewDidLoad function is not called?

I want to change UIBarButton's color in the navigationBar. To achieve this, in viewDidLoad: I put this line:
navigationController?.navigationBar.tintColor = .white
Everything works fine until I started to notice something strange. That UIBarButton is used to dismiss the UIViewController. When it is pressed, I just dismiss the viewController. But, if I present it (viewController) again, the color of the UIBarButton is not white, it gets tintColor of the application.
After doing some debugging, I noticed that viewDidLoad: is not called again after the viewController is just dismissed and presented again. The reason why my UIBarButton has a different color is because I change its color in viewDidLoad:. When viewDidLoad: is not called, of course, color is not changed.
It was an interesting discovery for me the fact that iOS doesn't call viewDidLoad: for UIViewController that was presented already. Possibly, it is due to the optimisation, because it is not efficient to draw the whole UI every time.
My solution to this problem can be to change color, not in viewDidLoad:, but in viewDidAppear:. But, is it right approach to solve a problem? And why viewDidLoad: is not called in the above situation?
It looks like you create and store you view controller, but present it wrapped in UINavigationController:
let controller = YourModalViewController()
...
func presentMyModal() {
present(UINavigationController(rootViewController: controller))
}
In this case your viewDidLoad method will be called just once and you'll have visual bug. If you want to leave styling code of your modal inside it's file you can create instance func which will return this controller wrapped and styled.
extension YourModalViewController {
func wrappedInNC() -> UINavigationController {
let nc = UINavigationController(rootViewController: controller)
// Styling code.
return nc
}
}

Detect if my controller is presented as popover

Using Xcode 10.1 and Swift 4.2.1, iOS 12
I got a tableview embedded in a navigation controller. In the upper right corner a bar button and a segue (popover) from this button to a navigation controller which holds a static table view, here the cells has further segues to other table views.
On an iPhone the static table view is displayed as a full screen modal, on an iPad it's a popover, which is okay so far.
I've set everything up in the storyboard, don't know if this could be the reason, but now I'm struggling to check if the view is a popover or a full screen modal.
I've tried:
print("\(presentationController)")
if presentationController is UIPopoverPresentationController {
// Do something
}
But this doesn't work -> print("\(presentationController)") gives me Optional(<_UIFullscreenPresentationController: 0x7fd00ad45770>) on iPhone and iPad.
I've tried also with:
if popoverPresentationController != nil {
print("popover")
}
But popover is printed if launched on iPhone or iPad.
Am I doing something wrong here or am I missing something?
Currently I'm using this:
if (popoverPresentationController?.arrowDirection != UIPopoverArrowDirection.unknown) {
tableView.sectionHeaderHeight = CGFloat.leastNormalMagnitude
tableView.sectionFooterHeight = CGFloat.leastNormalMagnitude
}
It's easy, and it works. In case someone has a real possibility to find out "if is a popover", you are very welcome to post it in here.
Use "UIModalPresentationStyle" of the presentedViewController to detect presentation style is fullscreen or popover.
You can find more details here: https://developer.apple.com/documentation/uikit/uimodalpresentationstyle
Simply you can check this in the presented ViewController
if self.modalPresentationStyle == .popover {
print("Popover presentation")
}
In case if the presented view controller is enclosed in a navigation controller, then use this
if self.navigationController?.modalPresentationStyle == .popover {
print("Popover presentation")
}

Insert subview behind TabBar from child view of TabBarController

I have TabBar with 2 tabs. At some point, from either of the 2 tabs, I want to add a view that is visible on both tab views but behind the TabBar.
So I thought, insert a subview into the TabBarController but below the TabBar.
This works fine in principle and I have the view behind the TabBar but now covering my 2 tabs as I wanted. However, it doesn't actually load. Just its background loads and only viewDidLoad() is called, not viewWillAppear() or any others.
I have also tried calling addChildViewController(myVC) on the TabBarController which has no effect, and also manually calling viewWillAppear() on the view controller I add which also has no effect (and I'm also dubious about whether manually calling viewWillAppear() is permitted or not?).
Is what I'm trying to do possible? What am I missing? Or should I be attempting this some other way?
For some reason, when inserting a subview into a UITabBarController behind it's UITabBar, although the view is visible to the user, the system itself seems to think it is not and so although viewDidLoad() is called, viewDidAppear() and subsequent methods are not.
However, adding a subview above the UITabBar seems to work fine. So I solved this by adding my own new UITabBar as a subview to the UITabBarController (set up basically exactly as the default one would be) and then removing the UITabBarController's default UITabBar.
Then when later inserting my view into the UITabBarController, I insert it as I was doing originally but instead below/behind my custom UITabBar and it seems to load fine.
There is no need to remove and recreate the tabBar. What you need to do is after you insert your custom view, you can then bring the tabBar to the front again.
//bring the tabBar to the front after inserting new view
self.view.bringSubview(toFront: self.tabBar)
This would be a good way:
Add the function below and call it in viewDidLoad of your initial VC. It unwraps your tab bar controller instance (which is optional), and then inserts the view you always want visible just below the tab bar.
private func setupAlwaysVisibleView() {
guard let tabBarController = self.tabBarController else { return }
tabBarController.view.insertSubview(alwaysVisibleView, belowSubview: tabBarController.tabBar)
}
Avoid using optionals for tabBarController or removing current tabBar. Simple add your view below tabBar view. Swift 5, XCode 11.
class TabBarController: UITabBarController {
#IBOutlet var instructionsView: UIView!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.view.insertSubview(instructionsView, belowSubview: self.tabBar)
}
}
you can also do this inside the init() method for your UITabViewController:
view.insertSubview(alwaysVisibleView, belowSubview: self.tabBar)
no need to dispatch to another method if you are using a subclass of UITabViewController.

UINavigationController without navigation bar?

I have a universal app, and on the iPad version I'm using UISplitViewController to create an interface similar to the Mail app.
I was having trouble pushing new Detail views, so I decided to use a UINavigationController so I could just push and pop views as needed. However, I do not want to use the navigation view or a toolbar. But no matter what I do, I can't hide the navigation bar.
I've tried unchecking "Shows Navigation Bar" in IB, and I've also tried setting:
[self.navigationController setNavigationBarHidden:YES];
in the viewDidLoad/viewDidAppear/viewWillAppear. I've also tried it in each of the views that will be pushed. Nothing works.
Is there something I'm missing here? Is it possible to have a UINavigationController without a toolbar or navigation bar?
You should be able to do the following:
self.navigationController.navigationBar.isHidden = true //Swift 5
where self.navigationController is (obviously) an instance of UINavigationController. Seems to work for me, but I only briefly tested it before posting this.
In Xcode 4.3.2:
Select the navigation controller in the storyboard
Select the Attributes Inspector in the (right) Utilities panel
Under the Navigation Controller category you have two check boxes:
[] Shows Navigation Bar
[] Shows Toolbar
Worked for me...
If you want no navigation bar, and you want the content to be adjusted up to where the navigation bar normally would be, you should use
self.navigationController.navigationBarHidden = YES;
This gives you a result like this:
Whereas self.navigationController.navigationBar.hidden = YES; gives you a space where the navigationBar should be. Like this:
Swift 4
I hide it in viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true;
}
Then you can put it back when you push a segue (if you want to have the back button on the next view)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
self.navigationController?.isNavigationBarHidden = false;
}
Swift 3 Programmatically
self.navigationController.isNavigationBarHidden = true
or
self.navigationController.navigationBar.isHidden = true
Note: I didn't see a difference between these two approaches testing on iOS 10.
All these answers still leave a space at the top for the status bar - add this line to remove that as well:
navController.navigationBar.isHidden = true
navController.accessibilityFrame = CGRect.zero

Resources