Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 754
AveLeon Avatar asked Jul 30 '13 23:07

AveLeon


People also ask

What is safeAreaLayoutGuide?

The safe area layout guide is a property of the UIView class and it inherits from the UILayoutGuide class. It was introduced in iOS 11 and tvOS 11. It helps you correctly position views in a layout. The safe area layout guide replaces the top and bottom layout guides of the UIViewController class.

What is a UIViewController?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.

How do I add a top layout guide?

In Figure 1: You can see constraint to Top Layout Guide and it will take statusbar as a base. Show activity on this post. Go to View Controller in interface Builder. And Click Show the attributes inspector and then Select Status Bar as Defaults Both will be same.

What is top layout guide in Swift?

Indicates the highest vertical extent for your onscreen content, for use with Auto Layout constraints.


1 Answers

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.

like image 54
Stephen Melvin Avatar answered Nov 25 '22 11:11

Stephen Melvin