After change navigation bar height, related constraint doesn't update - ios

I have navigation controller in my project. I want set its height based on view controller's main view height.If I could set this in IB then I don't face with issues, but I didn't find a way. So I use the following command in ViewDidAppear of my Navigation Controller class.
self.navigationBar.frame = CGRectMake(0, UIApplication.sharedApplication().statusBarFrame.size.height, self.view.frame.size.width, self.view.frame.size.height * 0.05)
I also have another view controller that use this navigation controller and have another child view inside it named ViewTop. Top of ViewTop equals to bottom of TopLayoutGuide. Because I have navigation bar,in real its top set to bottom of navigation bar.
Before I use the above command, ViewTop's top is equal to bottom of navigation bar correctly.
The problem occurs when navigation bar height changed via the above command. The top of ViewTop doesn't update to bottom of navigation bar. It seems that this change in height of navigation bar played after constraints calculations.
Also use above command in viewWillLayoutSubviews and viewDidLayoutSubviews of my Navigation Controller class with no success. I didn't set any other constraints in code and just used IB.
What Can I do?

Related

Unable to put view above navigation bar

I am trying to design a side menu, whose height would be equal to screen height (therefore hiding the navigation bar too). However I am unable to get the same.
I have put constraints as this:
and in viewDidLayoutSubviews()
I have mentioned - sideMenuTopConstraint.constant = -1 * (self.navigationController?.navigationBar.frame.height)!
However I see no change. Also by increasing the height of the side menu view in storyboard, I see that it is always below the nav bar. How do I make it appear above it?
You need to add the view to window as a subview, that should bring this view above the navigation bar as needed.
This is because window is the root of all the views.
UIApplication.sharedApplication().keyWindow?.addSubview(desiredViewHere)

Adjust Navigation Bar width inside NavigationController

I am trying to implement a horizontal scrolling table view. I was able to accomplish this with the following layout. I have a ScrollView which contains a Container View which points to my NavigationController. Since I made the Container View wider than the ScrollView, the table is able to scroll left to right as expected.
The problem I am having is resizing the Navigation Bar's width as I do not want to have to scroll to view the Navigation Bar's title. I tried explicitly setting the width property of the Navigation Bar in ViewDidAppear however it keeps getting resized to the actual width of the table view. Is there any way I can do this without having to create my own custom view that mimics that Navigation Bar?
I ended up creating my own navigation bar that is placed on top of the actual integrated table view navigation bar.

I want to place Table View exact down to navigation bar

Please see the dashed-box area, this is extra space appearing between navigation bar & table view.
I've tried 3 things but same problem.
1. Cleared all constrains & "Add missing constrains"
2. I've used pin, & keep '0' distance to UI object above table view
3. I've used dragging method, & set vertical spacing for table view.
You nee to uncheck the property inside attribute inspector Adjust Scroll View Insets using storyboard see image below.
Edit:
Adjust Scroll View Insets: Defines a Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
The default value of this property is true, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to false if your view controller implementation manages its own scroll view inset adjustments.
Select View Controller and Check Adjust scroll View insets.
I've done it, I was placing table view under navigation bar, now I've placed it exactly touching upper bounds of the view. And added missing constrains. enter image description here
Use this line in your tableview class's viewDidLoad method,
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
This sets the content from top.
It will be helpful for you.
-(void)viewDidLoad{
[super viewDidLoad:animated];
self.automaticallyAdjustsScrollViewInsets = NO;
}
By adding following line in viewDidLoad will solve this issue
self.navigationController?.navigationBar.translucent = false

Adding a view above UINavigationBar

I'm trying to add a custom view above the navigation bar as displayed on the following image .
What I've tried so far is to add the view as a subview of the navigation bar - Did not work
Add the custom view first on Interface builder and then add a navigation bar manually - This seems to mess up with my navigation as the default navigation controller has it's own navigation bar.
Is this a bad approach on its own ?
If this is a view that will appear above all navigation bars in the app, why not use a Container View?
Make the Container View fill the screen, but give it a 20 point top margin to fit your custom green view. Then when you add your main view controller (wrapped in a navigation view controller) to the Container View, it will show your green view above it.
If you don't want your margin in a particular screen, do the following:
Add a constraint to the top margin and add an outlet to it so you can modify the constraint's constant in code.
Then, in any screens where you don't need the margin, access the parent view controller view and set the constraint's constant to zero.
let myParentViewController = self.parentViewController as? MyParentViewController
myParentViewController.myTopMarginLayoutConstraint.constant = 0
Don't forget to reset it to 20 (or whatever) when you go to a screen where you need the top margin again.

Example of topLayoutGuide and bottomLayoutGuide UIViewController properties

I will appreciate if someone can show how to use the topLayoutGuide and bottomLayoutGuide properties of UITableViewController introduced in iOS 7. Thank you.
The topLayoutGuide and bottomLayoutGuide properties are inherited from UIViewController and implements the UILayoutSupport protocol. They are designed to be used with AutoLayout, but can also be used directly without the use of AutoLayout.
In the case of topLayoutGuide, the property indicates the highest vertical extent for content that you don't want to appear behind a translucent or transparent UIKit bar (such as a status or navigation bar). So, if your view controller has been pushed onto a UINavigationController stack, topLayoutGuide will tell you how many points from the top of the screen the status bar and navigation bar cover. You can use this value scroll content in a UIScrollView to just below the navigation bar or ensure other content in your view isn't being covered by the UINavigationBar.
Keep in mind that the value of topLayoutGuide depends on the view controller and whether or not it's contained within another view controller. From Apple's UIViewController documentation:
The object that constrains the value for this property depends on
whether or not the view controller is a child of a container view
controller (such as a navigation or tab bar controller), as follows:
• A view controller not within a container view controller constrains
this property to indicate the bottom of the status bar, if visible, or
else to indicate the top edge of the view controller's view.
• A view controller within a container view controller does not set this
property's value. Instead, the container view controller constrains
the value to indicate:
The bottom of the navigation bar, if a
navigation bar is visible
The bottom of the status bar, if only a
status bar is visible
The top edge of the view controller’s view, if
neither a status bar nor navigation bar is visible
Here's a piece of code I use to move a UITextField in response to the showing of the keyboard. I move the textfield to just below the navigation bar.
CGFloat length = self.topLayoutGuide.length;
_feedback.frame = CGRectMake(_feedback.frame.origin.x, length + 5.0, _feedback.frame.size.width, _feedback.frame.size.height);
Using bottomLayoutGuide is exactly like using topLayoutGuide, except the bottomLayoutGuide refers to the lowest vertical extent for content.

Resources