Programming
Explaining difference between automaticallyAdjustsScrollViewInsets extendedLayoutIncludesOpaqueBars edgesForExtendedLayout in iOS7
Understanding the nuances of iOS 7 layout can be tricky, especially when dealing with properties like automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, and edgesForExtendedLayout. These properties control how your view controllers interact with navigation bars, tab bars, and other translucent or opaque bars, significantly impacting the visual presentation of your app. Many developers, even experienced ones, often grapple with their intended behavior and how they affect the overall layout of scroll views and other content. Properly configuring these properties is essential for creating a polished and user-friendly iOS application, preventing content from being obscured or displaying incorrectly. This article delves into the specifics of each property, providing clear explanations, practical examples, and guidance on when and how to use them effectively to achieve the desired layout in your iOS 7 projects and beyond.
Understanding automaticallyAdjustsScrollViewInsets
The automaticallyAdjustsScrollViewInsets property, a feature introduced in iOS 7, is designed to simplify the management of scroll view content insets when using translucent navigation bars or tab bars. When set to YES (the default value), the view controller automatically adjusts the contentInset and scrollIndicatorInsets properties of its scroll view to account for the presence of these bars. This ensures that the scroll view’s content is not obscured by the navigation bar or tab bar at the top and bottom of the screen.
However, the automatic adjustment might not always be desirable, especially when you want fine-grained control over the scroll view’s insets or when you’re using custom navigation bar configurations. In such cases, setting automaticallyAdjustsScrollViewInsets to NO allows you to manually manage the insets, providing greater flexibility in designing your user interface. For instance, if you have a custom header view that should always be visible above the scrollable content, disabling automatic adjustment and setting the insets manually would be the appropriate approach. Remember that inconsistencies in handling these insets can lead to frustrating UI bugs, making understanding this property crucial for iOS development.
It’s important to note that this property primarily affects scroll views that are the first subview of the view controller’s view, or those that are constrained to the edges of the view controller’s view using Auto Layout. According to Apple’s documentation, improper use can lead to unexpected clipping or content overlap. To avoid issues, carefully consider your layout requirements and the presence of translucent bars when deciding whether to enable or disable automaticallyAdjustsScrollViewInsets.
Exploring extendedLayoutIncludesOpaqueBars
The extendedLayoutIncludesOpaqueBars property determines whether the view controller’s view extends under opaque bars. By default, this property is set to NO, meaning the view controller’s view is positioned below any opaque bars, such as a navigation bar with a fully opaque background. Setting it to YES allows the view to extend behind these bars. This can be useful for creating visually appealing effects, such as a background image that spans the entire screen, including the area behind the navigation bar.
However, enabling extendedLayoutIncludesOpaqueBars requires careful consideration of content placement. If you simply extend the view behind the opaque bar without adjusting the content insets, the content may be obscured. You’ll likely need to manually adjust the contentInset of any scroll views or other content views to ensure that the content is visible and accessible. For example, if you want a full-screen background image with a table view overlaying it, setting this property to YES and then adjusting the table view’s contentInset is crucial.
A practical use case is in apps that aim for a modern, immersive design. Setting extendedLayoutIncludesOpaqueBars to YES can allow you to create a seamless visual experience. Remember that using this property effectively often involves combining it with adjustments to automaticallyAdjustsScrollViewInsets and edgesForExtendedLayout to achieve the desired layout behavior. “Apple’s Human Interface Guidelines emphasize creating a consistent and predictable user experience. Misuse of layout properties can lead to a disjointed feel,” says UI design expert John Sundell. Swift by Sundell offers more insights on these topics.
Delving into edgesForExtendedLayout
The edgesForExtendedLayout property offers fine-grained control over which edges of the view controller’s view extend under translucent bars. This property accepts a UIRectEdge value, which can be one of the following: .None, .Top, .Bottom, .Left, .Right, or .All. By default, it’s set to .All, allowing the view to extend under translucent navigation bars, tab bars, and other bars on all edges of the screen.
The power of edgesForExtendedLayout lies in its ability to selectively control which edges extend under bars. For example, if you want the view to extend only under the navigation bar at the top of the screen, you can set edgesForExtendedLayout to .Top. This can be useful when you have a custom tab bar or other bottom bar that you don’t want the view to extend behind. It is a very targeted way to adjust the layout of your views.
Here is a featured snippet-optimized paragraph: When dealing with complex layouts or custom bar implementations, edgesForExtendedLayout becomes invaluable. By selectively specifying which edges should extend under translucent bars, developers can avoid unintended content clipping or overlapping. For instance, setting edgesForExtendedLayout to UIRectEdge.None ensures that the view’s content respects the boundaries of the navigation bar and tab bar, preventing any extension underneath them. This level of control is particularly beneficial in apps with highly customized user interfaces.
Practical Examples and Use Cases
Let’s examine some practical examples to illustrate how these properties work in real-world scenarios.
- Scenario 1: Full-Screen Image with Translucent Navigation BarTo display a full-screen image behind a translucent navigation bar, set
extendedLayoutIncludesOpaqueBarstoYESandedgesForExtendedLayoutto.All. You might also need to adjust the scroll view’scontentInsetto prevent the image from being obscured. This approach creates an immersive visual experience. - Scenario 2: Table View with Opaque Navigation BarIf you have a table view with an opaque navigation bar, set
extendedLayoutIncludesOpaqueBarstoNOandautomaticallyAdjustsScrollViewInsetstoYES. This ensures that the table view’s content starts below the navigation bar and that the scroll view’s insets are automatically adjusted. This is the standard approach for most table view-based apps. - Scenario 3: Custom Header View with Scroll ViewWhen using a custom header view above a scroll view, set
automaticallyAdjustsScrollViewInsetstoNOand manually calculate the scroll view’scontentInsetto accommodate the header view and any translucent bars. This gives you precise control over the scroll view’s layout.
Consider an e-commerce app displaying product images. They might use the full-screen image approach to create a visually appealing product detail view. Conversely, a news app with a persistent navigation bar would likely use the table view approach to ensure content is clearly visible. Understanding these scenarios helps developers choose the right configuration for their specific needs.
- Key Takeaway 1:
automaticallyAdjustsScrollViewInsetssimplifies scroll view management but can be limiting. - Key Takeaway 2:
extendedLayoutIncludesOpaqueBarsallows views to extend behind opaque bars for visual effects.
- Q: When should I disable `automaticallyAdjustsScrollViewInsets`?
- A: Disable it when you need fine-grained control over scroll view insets or when you're using custom navigation bar configurations. If the automatic adjustments are interfering with your desired layout, manual control is the way to go.
- Q: What happens if I set `extendedLayoutIncludesOpaqueBars` to `YES` without adjusting content insets?
- A: Your content may be obscured by the opaque bar. Always adjust content insets to ensure visibility.
- Q: How does `edgesForExtendedLayout` interact with `automaticallyAdjustsScrollViewInsets`?
- A: `edgesForExtendedLayout` controls which edges extend under translucent bars, while `automaticallyAdjustsScrollViewInsets` adjusts scroll view insets to account for these bars. They work together to determine the overall layout.
Best Practices and Troubleshooting
When working with these layout properties, it’s essential to follow best practices to avoid common pitfalls. Always test your layout on multiple devices and screen sizes to ensure consistent behavior. Use Auto Layout constraints to define the relationships between views, making your layout more flexible and adaptable. Carefully consider the implications of each property setting and adjust content insets accordingly.
- Always test on multiple devices.
- Use Auto Layout effectively.
If you encounter issues, start by examining the view hierarchy and the values of these layout properties. Use the Xcode debugger to inspect the contentInset and scrollIndicatorInsets of your scroll views. Experiment with different combinations of property settings to understand their effects. Consult Apple’s documentation and online forums for additional guidance. If you are still having trouble, consider asking for help from other developers on platforms like Reddit’s iOSProgramming community.
Understanding and properly utilizing automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, and edgesForExtendedLayout is crucial for crafting well-designed iOS applications. Mastering these properties ensures that your content is displayed correctly, regardless of the presence of translucent or opaque bars. By following the guidelines and examples provided in this article, you can confidently tackle even the most complex layout challenges.
Now that you have a solid understanding of these iOS 7 layout properties, take what you’ve learned and apply it to your projects. Experiment with different configurations, test thoroughly, and don’t hesitate to explore further resources. A well-crafted user interface is key to a successful app, and mastering these layout properties is a significant step in that direction. Consider exploring related topics like Auto Layout best practices or advanced scroll view techniques to further enhance your iOS development skills. Explore more of our iOS development resources here.
Question & Answer :
I have been reading a lot about iOS7 UI transition.
I am not able to get what these three properties automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout??
For example I am trying to make my view controllers start below the status bar but I am not able to achieve it.
Starting in iOS7, the view controllers use full-screen layout by default. At the same time, you have more control over how it lays out its views, and that’s done with those properties:
edgesForExtendedLayout
Basically, with this property you set which sides of your view can be extended to cover the whole screen. Imagine that you push a UIViewController into a UINavigationController. When the view of that view controller is laid out, it will start where the navigation bar ends, but this property will set which sides of the view (top, left, bottom, right) can be extended to fill the whole screen.
Let see it with an example:
UIViewController *viewController = [[UIViewController alloc] init]; viewController.view.backgroundColor = [UIColor redColor]; UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
Here you are not setting the value of edgesForExtendedLayout, therefore the default value is taken (UIRectEdgeAll), so the view extends its layout to fill the whole screen.
This is the result:

As you can see, the red background extends behind the navigation bar and the status bar.
Now, you are going to set that value to UIRectEdgeNone, so you are telling the view controller to not extend the view to cover the screen:
UIViewController *viewController = [[UIViewController alloc] init]; viewController.view.backgroundColor = [UIColor redColor]; viewController.edgesForExtendedLayout = UIRectEdgeNone; UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
And the result:

automaticallyAdjustsScrollViewInsets
This property is used when your view is a UIScrollView or similar, like a UITableView. You want your table to start where the navigation bar ends, because you wont see the whole content if not, but at the same time you want your table to cover the whole screen when scrolling. In that case, setting edgesForExtendedLayout to None won’t work because your table will start scrolling where the navigation bar ends and it wont go behind it.
Here is where this property comes in handy, if you let the view controller automatically adjust the insets (setting this property to YES, also the default value) it will add insets to the top of the table, so the table will start where the navigation bar ends, but the scroll will cover the whole screen.
This is when is set to NO:

And YES (by default):

In both cases, the table scrolls behind the navigation bar, but in the second case (YES), it will start from below the navigation bar.
extendedLayoutIncludesOpaqueBars
This value is just an addition to the previous ones. By default, this parameter is set to NO. If the status bar is opaque, the views won’t be extended to include the status bar, even if you extend your view to cover it (edgesForExtendedLayout to UIRectEdgeAll).
If you set the value to YES, this will allow the view to go underneath the status bar again.
If something is not clear, write a comment and I’ll answer it.
How does iOS know what UIScrollView to use?
iOS grabs the first subview in your ViewController’s view, the one at index 0, and if it’s a subclass of UIScrollView then applies the explained properties to it.
Of course, this means that UITableViewController works by default (since the UITableView is the first view).