View constraints change when UINavigation's Translucent is set to false - ios

When setting UINavigationBar's Translucent property to false/ (unchecked in storyBoard). The constraints doesn't work as expected.
Functionality of the ViewController: Custom camera module displaying the camera preview on a UIView.
View Hierarchy
ViewController's View
PreviewView (UIView) for displaying camera preview.
The constraints between PreviewView & View are below.
When UINavigationBar is not Translucent
As you can see the constraints are set to equal width & height with the superview but the result is not the same. The screenshot is landscape/iPad but the result is same even on portrait.
When UINavigationBar is translucent
On the other hand when the UINavigationBar is translucent the constraints work fine.

Please check this Select View Controller -> IB Inspector -> (Extend Edges) -> Under Top Bars Please deselect this and try.

Didn't work for me either.
Changing the navbar to isTranslucent = true changes the view layout. If isTranslucent == false, the view origin.y is at 0.0 (at top of screen), but if isTranslucent == true, view origin.y is at (navbar height) meaning view starts below the navbar. View height is reduced accordingly.
Why is this happening?
My fix was to adjust layout code to accomdate this change, but it seems odd that a visual change would cause this behavior.

Related

UIScrollView content insets for iPhoneX (safeAreaLayoutGuide)

I have a UIScrollView that is constraint to view.topAnchor, so it scrolls up to the top edge of an iPhoneX. However, when I add content to the scrollView (such as a UIImage), and constraint it scrollView.topAnchor the content is inset to the safeAreaLayoutGuide. This seems to happen with scrollViews since if I take the content out and place it in the view, it also moves to the screen edge.
Anyone have any ideas how to fix this?
Try setting the view controller's automaticallyAdjustsScrollViewInsets to false in viewDidLoad (self.automaticallyAdjustsScrollViewInsets = false). From the documentation:
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.
UPDATE:
As was mentioned, automaticallyAdjustsScrollViewInsets is deprecated in iOS 11+. You can do a version check and to fix it for older versions of iOS as well. Example:
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}

Scrollview Autolayout with Tabbar and Navigationbar

I need to set constraints in uiscrollview in viewcontroller containing navigationbar and tabbar.I have placed a uiview(contentView) inside scrollview which is of same height as of scrollview.These are the things which I have set curently,
set 4 constraints for scrollview(top,bottom,left,right) with
constraints to margin unchecked which is 0 for all edges.
set 4 constraints for uiview(contentView) inside
scrollview(top,bottom,left,right) with constraints to margin
unchecked which is 0 for all edges.
set equal width and equal height
for scrollview and UIView(contentView)
output is displaying like this uiview(contentView) gets placed lower about 64 px (approx).This view should not place like this.Can anyone help me to solve this.
Here is the project demo which I have worked and can be downloaded here
Select the scene and in the attributes inspector, uncheck: "Under Top Bars" and "Under Bottom Bars".
Then in the storyboard make the scroll view start at the very top of the View Controller, where the navigation bar starts, and then the UIView, where the navigation Bar ends.
Later, select the Scroll View and set the constraints as they come by default.
I guess it's not the cleanest way to do it but I had the same issue and worked for me.
You might try self.automaticallyAdjustsScrollViewInsets = false
The Tab Bar has to be set in the tree UNDER the scrollView, like this:
Try this
override func viewDidLoad() {
self.edgesForExtendedLayout = UIRectEdge()
self.extendedLayoutIncludesOpaqueBars = false
}
I have found the solution for it,The parent ViewController class is set to under top bar and disabling it and changed the frame accordingly in the parent and set the same property and frame to child viewcontroller fixed the issue.

Autolayout with Navigation Bar - Remove Specific Constraint

I'm building an application for iOS, which is using a navigation controller. I want to put a view in the titleView, and have it fill the whole width of the navigation bar.
I'm calling setupNavBar in viewDidLoad of the view controller that is embedded in the navigation controller. Here is how I do:
func setupNavBar() {
let navBar = navigationController?.navigationBar
// navBar!.translatesAutoresizingMaskIntoConstraints = false
// navBar!.frame.size.height = CGFloat(100)
let searchBar = UIView(frame: navBar!.frame)
searchBar.bounds = CGRectMake(0, 0, navBar!.frame.width, navBar!.frame.height)
searchBar.backgroundColor = UIColor.brownColor()
navigationItem.titleView = searchBar
}
But the view (brown - "searchBar"), doesn't cover the full navigation bar:
So I figured out that the problem was related to Autoresizing and Constraints, because if I call navBar!.translatesAutoresizingMaskIntoConstraints = false, I can freely set the sizes of views frame, so there must be some constraints that change the view's bounds. But I would like to keep as much of the autolayout behaviour as possible.
Is there a way to only change the contraints on the titleView?
I'm doing everything programmatically, I don't use the storyboard or xib's!
EDIT:
But it doesn't seem like there is any constraints on either navigationItem or navBar:
for someObject in navigationItem.titleView!.constraints {
print(someObject)
}
It doesn't print any constraints. neither if I use navBar.constraints!
EDIT 2:
I have a screenshot from "View UI Hierarchy" from the debug navigator:
It seems that the view(brown) alligns with the Navigation Bar Back Indicator View, maybe this is a clue to what causes the problem?
How come the view is resized?
Two different suggestions here:
1. You can try the
navigationItem.titleView.sizeToFit()
Otherwise you could set the background colour of the navbar to brown as it appears you wish the brown bar to cover the entire width of the navbar. If you want to add other views on top of that you then can.
You could also try to make an outlet to the title view and add an NSLayoutConstraint using
navigationItem.titleView.addConstraint(NSLayoutConstraint: NSLayoutConstraint)
I am not entirely sure whether that will work, however.

UIScrollview position moves after touches begin

I view imbedded in a Navigation Controller which is slightly transparent. This view contains a UIscrollview. This UIScrollview has the following subviews: UIImageView, UILabel, and UIWebview. All these other views are working correctly. When the view loads, everything is in the correct position (The views are below the Navigation Bar). However, as soon as the screen is tapped, the scrollview repositions itself to the top of the view (aka behind the Navigation Bar, which is at the top, and I can see that this is true because the Nav Bar is slightly transparent)
Any idea why this is happening? I set up the scrollview like this:
self.scrollView.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.window.frame.size.width, self.view.window.frame.size.height);
Also set these properties:
self.scrollView.scrollEnabled = YES;
self.scrollView.alwaysBounceVertical = YES;
self.scrollView.bounces = YES;
Anybody know why this is happening?
I'd similar problem, but only on iOS 7.0. Try to set the translucent = NO property of the navigationBar. This will make the navigationBar opaque. Although if you want the navigationBar to be transparent, try to change the contentInset of the scrollView to adjust the content as you want. Hope that this will help :)

iOS 7 TableView in a ViewController and NavigationBar blurred effect

I started building a TableView in my app by using a TableViewController in a storyboard. When you do this, you have a very cool effect when you scroll down your list : the cells moving behind the nav bar get blurred.
Some time later, I had to move from this TableViewController to a ViewController with a TableView inside (I had to add other views at the bottom of the table).
In order to avoid having the first cells hidden by the navigation bar (being over it), I added constraints to the Top and Bottom Layout Guides, and to the left and right edges of the view.
This works fine, but I lost the cool blurred scrolling effect : the cells seem to be disappearing before going behind the navigation bar.
I've seen workarounds with people not using constraints and putting magic numbers in interface builder. I cannot do this, first because I dislike it, and second because I have to be iOS 6 compatible.
What did I miss to be able to benefit again from the blurred navigation bar effect ?
You have to manually adjust the contentInset of the table view and make sure the table view frame origin is 0, 0.
In this way the table view will be below the navigation bar, but there will be some margin between the content and the scroll view edges (the content gets shifted down).
I advise you to use the topLayoutGuide property of the view controller to set the right contentInsets, instead of hard coding 64 (status bar + navigation bar).
There's also bottomLayoutGuide, which you should use in case of UITapBars.
Here is some sample code (viewDidLoad should be fine):
// Set edge insets
CGFloat topLayoutGuide = self.topLayoutGuide.length;
tableView.contentInset = UIEdgeInsetsMake(topLayoutGuide, 0, 0, 0);
By the way, this properties of UIViewController might help you (you should not need to change their default values, but I don't know what your view hierarchy is):
automaticallyAdjustsScrollViewInsets
edgesForExtendedLayout
extendedLayoutIncludesOpaqueBars
The tableView needs to be full screen. That is underneath the top and bottom bars. Note don't use the top and bottom layout guides as they are used for positioning relative to the bars not underneath.
Then you need to manually set the content inset of the tableview. This sets the initial scroll position to under the top bar.
Something like:
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
CGFloat h=MIN(statusBarSize.width, statusBarSize.height);
UIEdgeInsets e = UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height + h,
0.0f,
0.0f,
0.0f);
self.tableView.contentInset = e;
Not you get this functionality for free when using a tableView controller and the "Automatically Adjust content inset" settings
You probably have the coordinates of your tableView not set to (0, 0) to map to those of the viewController.view.frame or viewController.view.bounds. If you have done that, try setting
self.navigationController.navigationBar.translucent = YES;
UIViewController property edgesForExtendedLayout does the trick. If you are using storyboards just make sure Extended Edges Under Top Bars is on (and it is by default).
If you are creating your view controller programmatically try this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeAll;
}
And of course, your table view needs to have proper autoresizing mask/layout constraints
edgesForExtendedLayout is not what you want here, as this will limit the table view underneath the navigation bar. In iOS 7, the view controllers uses fullscreen by default, and the property controlling where the tableview content starts is automaticallyAdjustsScrollViewInsets. This should be YES by default, so check if it is somehow set to NO, or try setting it explicitly.
Check this answer for a good explanation on how this works:
https://stackoverflow.com/a/19585104/1485715

Resources