How to reproduce this "offset behavior" programmatically? - ios

Since iOS 7, Apple encourages developers to show content behind navigation bars and other translucent elements.
Let's say that I have a view controller embedded in a navigation view, adding to this view controller a map view and extending the edges of the map view to the bounds of the screen (so even behind the navigation bar) gives the expected effect : the navigation bar blurs the content of the map view.
But iOS also does something else : although the map itself is rendered behind the navigation bar, if a ui element the user must see or touch (for example the compass) is actually offseted so that it isn't hidden by the navigation bar ; a picture is worth a thousand words :
What I would like to know is what property of the map view I should use to make it behave like this programmatically ? (because if I manually add a translucent view on the top of my map, the compass will be hidden, so I would like to be able to do that) ? This question also applies for other kinds of views, such as table views, is it the same principle ?
Thank you.

For UIScrollView and its subclasses (UICollectionView, UITableView), setting the contentInset to something like, say, UIEdgeInsetsMake(64.0 /* TOP */, 0.0 /* LEFT */, 44.0 /* BOTTOM */, 0.0 /* RIGHT */); would be the way to go.
You can inspect the UIViewController's properties topLayoutGuide and bottomLayoutGuide to determine how much you have to inset.
For MKMapView, I've tried adding it programmatically and it appear that it adjusts the frame of the compass and the "Legal" label automatically. I would say that it takes topLayoutGuide and bottomLayoutGuide into account to layout the button and label.

Related

Add a UINavigationBar to a UIView using Autolayout

I have a modal view that I'm presenting over the current view that has a UINavigationBar as a subview. Everything's working great, except that the UINavigationBar doesn't want to behave according to the autolayout constraints. Everything else in the view behaves as desired.
For some reason, the left and right bar button items are cut off on the edges of the view. I inspected the view at runtime to reveal that the total width of the UINavigationBar is the right width, but the bar buttons aren't conforming.
I also checked to make sure I didn't have negative margins or some other anomaly in the constraint definitions.
I have also tried setting the frame of the UINavigationBar manually, but it yielded no improvement.
Problematic View
Constraints
I'd like to keep the UINavigationBar, because it gets styled globally with the rest of my app (as opposed to creating a custom UIView imitation and styling it individually). Does anyone know what handle to jiggle to get the bar button items to conform to the right width?

MKMapView Content Insets with Navigation Bar

I'm experiencing a strange issue with MKMapView placed in a view with a navigation bar. Given a UIScrollView in the same situation, whose width, height, and center are constrained to it's superview (the view controller's view object) the insets will be adjusted if the controller's automaticallyAdjustsScrollViewInsets property is set to YES, and not if set to NO. That's great. I can not find this documented anywhere, but the map view seems to do the same thing, but it's not possible to disable it. Setting automaticallyAdjustsScrollViewInsets has no effect.
In brief(ish):
In the image below, the small back square is centered within the view, regardless of navigation bar. The map view is also constrained to be exactly the size of the view (it extends behind the nav bar). The location of the map view was set to be the current location (also indicated by the blue circle) so I would expect them to match, but they are off by half the combined height of status and navigation bars.
Is there an explanation for this behavior, and a way to get it to work as expected?
This is very strange because an MKMapView is not a subclass of a UIScrollView but it is behaving like one in this case. That's not completely unreasonable, but I would expect the auto adjusting insets to work consistently on a map view and a scroll view.
Workaround
Something awful like this would 'solve the problem':
MKCoordinateRegion region = MKCoordinateRegionMake(self.coordinate, MKCoordinateSpanMake(0.008, 0.008));
MKMapRect mapRect = MKMapRectForCoordinateRegion(region);
[self.mapView setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(0, 0, 64, 0) animated:YES];
Either adjusting the bottom inset as well, or adding a negative inset on the top gets things lined up, but it would break if this behavior of the map view changes, or if the view controller is presented without a navigation bar, or if there is a stiff breeze. It also erodes my confidence in pulling correct coordinates back out of the map view.
MKMapView does indeed sit under the navigation bar, but automatically uses a housing view controller's topLayoutGuide to shift annotations and the compass view, etc. down from under it.
You should make sure that your own views take this into account in their autolayout code.
If you take a look at this, you can see that the mapview is very likely not sitting under the navigation bar, as you think. It is sitting between the bottom of the nav bar and the bottom of the superview.
On another note, the navigation bar is translucent by default, so if the mapview was really sitting under the nav bar you would be able to see through it, assuming you didn't disable the default nav bar behavior.

why does Ios ignores top bar and layout items below it?

I am designing a screen using IB and in design time this is how the screen looks like:
I am using auto layout and there are no missing constraints. This is how it lookes like when I run it using the simulator:
Its a sit it has no idea there is a opaque top bar there and starts layouting the views directly from the top.
does anyone has any idea why this is happening?
Thanks
The size, status bar, orientation, top bar, bottom bar are only simulated at the attributes inspector , e.i. What u see, is NOT what u get, it's just simulate...
To make status bar/bottom bar/orientation etc. U must do it via code or .xib attributes inspector/size inspector.
For status bar, u can see this.
Hope it's help.
Add the following code to viewDidLoad in you view controller:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
You can achieve this by implementing a new property called edgesForExtendedLayout in iOS7 SDK. Please add the following code to achieve this,
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
You need to add the above in your -(void)viewDidLoad method.
iOS 7 brings several changes to how you layout and customize the appearance of your UI. The changes in view-controller layout, tint color, and font affect all the UIKit objects in your app. In addition, enhancements to gesture recognizer APIs give you finer grained control over gesture interactions.
Using View Controllers
In iOS 7, view controllers use full-screen layout. At the same time, iOS 7 gives you more granular control over the way a view controller lays out its views. In particular, the concept of full-screen layout has been refined to let a view controller specify the layout of each edge of its view.
The wantsFullScreenLayout view controller property is deprecated in iOS 7. If you currently specify wantsFullScreenLayout = NO, the view controller may display its content at an unexpected screen location when it runs in iOS 7.
To adjust how a view controller lays out its views, UIViewController provides the following properties:
edgesForExtendedLayout
The edgesForExtendedLayout property uses the UIRectEdge type, which specifies each of a rectangle’s four edges, in addition to specifying none and all. Use edgesForExtendedLayout to specify which edges of a view should be extended, regardless of bar translucency. By default, the value of this property is UIRectEdgeAll.
extendedLayoutIncludesOpaqueBars
If your design uses opaque bars, refine edgesForExtendedLayout by also setting the extendedLayoutIncludesOpaqueBars property to NO. (The default value of extendedLayoutIncludesOpaqueBars is NO.)
automaticallyAdjustsScrollViewInsets
If you don’t want a scroll view’s content insets to be automatically adjusted, set automaticallyAdjustsScrollViewInsets to NO. (The default value of automaticallyAdjustsScrollViewInsets is YES.)
topLayoutGuide, bottomLayoutGuide
The topLayoutGuide and bottomLayoutGuide properties indicate the location of the top or bottom bar edges in a view controller’s view. If bars should overlap the top or bottom of a view, you can use Interface Builder to position the view relative to the bar by creating constraints to the bottom of topLayoutGuide or to the top of bottomLayoutGuide. (If no bars should overlap the view, the bottom of topLayoutGuide is the same as the top of the view and the top of bottomLayoutGuide is the same as the bottom of the view.) Both properties are lazily created when requested.

How to make a UITableViewController subclass respect topLayoutGuide in iOS7?

in my app I have a simple UITableViewController that's just plain Objective-C code, no .xib or storyboard involved. It represents the contents of one tab in a tab bar.
Since iOS 7 its contents are overlapped by the status bar at the top and tab bar at the bottom.
Using only code, how can I make the table view add space at the top and bottom to align with topLayoutGuide and bottomLayoutGuide?
I know about
self.edgesForExtendedLayout=UIRectEdgeNone;
but that seems to simply shrink the table view to not intersect the tab bar and to disable the transparency of the tab bar. Instead I'd like the table view to add some padding.
Thanks!
Update:
I've also tried explicitly setting automaticallyAdjustsScrollViewInsets to YES, but that didn't help either (should be the default behavior anyway).
It seems this is not (yet) supported, at least for programmatically created UITableViewControllers without an UINavigationController that's embedding them.
I checked the position for both layout guides, and at run-time both off-sets read 0 distance from the edges of the screen. Hence automaticallyAdjustsScrollViewInsets won't set the insets correctly.
So now I actually modify my first section header and last section footer manually to add 21 pixels at the top and 50 pixels at the bottom respectively.
Bummer. :-(

In Xcode 5 in a GLKViewController in IB what are the Top/Bottom Layout guides for?

In Xcode 5 in a GLKViewController in IB what are the Top/Bottom Layout guides for? I've been reading through docs and can't seem to find it anywhere.
These are not specific to GLKViewController -- you'll see them in any view controller.
In iOS 7, views extend underneath the transparent status, navigation, and tab bars (if present) by default. Because you probably don't want your subviews ending up underneath these bars, the layout guides provide an easy way to set up layout constraints relative to wherever those bars end up.
For example, say you want a button to appear 20pt below the navigation bar. Where before you'd make a constraint between the button and the top of the view, you can now make a constraint between the button and the top layout guide. That view in the nib isn't responsible for setting whether the navigation bar is shown or how tall it is -- those things are controlled by the view controller that presents the view at run time -- so constraining to the top layout guide makes sure your button is in the right place regardless of how the view is presented.

Resources