UIView and Uiviewcontroller new related APIs since AutoLayout

Source: Internet
Author: User
Tags new set

http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/

UilayoutsupportJavaSince iOS 7, when we have navigationbar,tabbar or toolbar in our view controller structure, the default value of their translucent property has been changed to Yes, And the height of the current viewcontroller will be the height of the entire screen. (such as a scene: When you drag TableView, the above Navigationbar can see the contents of TableView. )
@property (nonatomic,readonlyID<UILayoutSupport> toplayoutguide ns_available_ios (7_0) ;  @property (nonatomic,readonlyID<UILayoutSupport> bottomlayoutguide ns_available_ IOS (7_0);   @protocol Uilayoutsupport <NSObject>  @property (nonatomic,readonly) cgfloat length;   @end

Since iOS 7, when we have navigationbar,tabbar or toolbar in our view controller structure, the default value of their translucent property has been changed to Yes, And the height of the current viewcontroller will be the height of the entire screen. (such as a scene: When you drag TableView, the above Navigationbar can see the contents of TableView. )

To ensure that our views are not covered by these bars, we can use both the Toplayoutguide and Bottomlayoutguide properties in our AutoLayout layout. Like this:

Nsdictionary *views = @{"toplayoutguide"@ "myLabel"  : MyLabel};  [Nslayoutconstraint Constraintswithvisualformat: @" V:[toplayoutguide]-[myview] " options:0

At this point our view is not covered by bar and is displayed below bar:

And with this property layout, its value changes dynamically when the traitcollection changes (rotating the screen). The above code, in the case of horizontal screen, the Navigationbar height has changed, still can display correctly.

These two guides are calculated in the following way:

Toplayoutguide is calculated by calculating the distance from the view Controller->view->top to the Bar (like navigation bar), which covers the lowest level of the view
Bottomlayoutguide is calculated by calculating the distance from the view Controller->view->bottom to the top of the bar (like Tab bar) on the top of the view


If we do not use the AutoLayout layout, we can also obtain the corresponding distance through the length property of the guide. We should call super after-viewdidlayoutsubviews or-layoutsubviews to get the length value to ensure it is correct.

Uiconstraintbasedlayoutcoremethods

-(void

A new method for updating layout constraints is also added in Uiviewcontroller, which details the UIView set of methods for updating layout constraints in the notes of the AutoLayout UIView related API.

The default implementation of this method is to invoke the-updateconstraints of the corresponding view. The Viewcontroller view will call the Viewcontroller Updateviewconstraints method when updating the layout of the views. Instead of inheriting the view to rewrite the-updateconstraints method, we can override this method to update the internal layout of the current view. When we override this method, be sure to call super or call the-updateconstraints method of the current view.

Uitraitenvironment

See the Uitraitenvironment protocol again, in the Uikit framework, there are four classes that support this Protocol, namely UIScreen, Uiviewcontroller,uiview, and Uipresentationcontroller. So when the traitcollection of the view changes, Uiviewcontroller can capture the message and do the corresponding processing. More explanations can be found in the previous article on the coordinates of Uicoordinatespace and UIScreen on iOS 8.

Refer to the following links for the concept of size class and uitraitcollection:

WWDC Session Notes –ios interface development Unification From:onecat ' s Blog

IOS8 the understanding and use of Size classes From:joywii ' s Blog

In addition, Uiviewcontroller provides the following two methods:

-(void) Setoverridetraitcollection: (Uitraitcollection *) Collection Forchildviewcontroller: ( Uiviewcontroller *) Childviewcontroller Ns_available_ios (8_0);  

We can reset the value of traitcollection for its childviewcontroller by calling Viewcontroller's Setoverridetraitcollection method. In general, the Traitcollection value is passed from the parent controller to the child controller without modification. When we implement a container controller ourselves, we can use this method to make adjustments.

In contrast, we can get the traitcollection value of Childviewcontroller by Overridetraitcollectionforchildviewcontroller method.

Uicontentcontainer

On IOS 8 with the concept of size class, Uiviewcontroller supports a new set of protocols such as Uicontentcontainer:

- (void) Systemlayoutfittingsizedidchangeforchildcontentcontainer: (ID<UIContentContainer>) Container Ns_available_ios (8_0); -(Cgsize) Sizeforchildcontentcontainer: (ID<UIContentContainer>) Container withparentcontainersize: (cgsize) parentsize Ns_available_ios (8_0); - (void) Viewwilltransitiontosize: (cgsize) size withtransitioncoordinator: (ID<UIViewControllerTransitionCoordinator>) Coordinator Ns_available_ios (8_0); - (void) Willtransitiontotraitcollection: (uitraitcollection *) newcollection Withtransitioncoordinator: (ID<UIViewControllerTransitionCoordinator>) Coordinator Ns_available_ios (8_0);

Uiviewcontroller provides a default implementation for this set of protocols. We can rewrite these methods to adjust the layout of the view when we customize Viewcontroller, for example, we can adjust the position of childviewcontroler in these methods. When we rewrite these protocol methods, we usually call super.

This method is triggered when the size of the Viewwilltransitiontosize:viewcontroller view is changed by his parent controller. (Rootviewcontroller, for example, when its window rotates). When we override this method, make sure to call super to make sure that the message of size change is passed to its views or childviewcontrollers normally.

Willtransitiontotraitcollection: This method is called when the value of Viewcontroller's traitcollection is about to change. This method is called before the Uitraitenvironment protocol method Traitcollectiondidchange: When we override this method, we also make sure to call super to guarantee the delivery of the message. For example, we can adjust the corresponding animation of the view when the Traitcollection value changes:

-(void) Willtransitiontotraitcollection: (uitraitcollection *) newcollection            Withtransitioncoordinator: (ID <UIViewControllerTransitionCoordinator>) Coordinator  {      [Super Willtransitiontotraitcollection:newcollection               withtransitioncoordinator:coordinator];      [Coordinator Animatealongsidetransition:^ (ID <UIViewControllerTransitionCoordinatorContext> context) {      if (newcollection.verticalsizeclass = = uiuserinterfacesizeclasscompact)      { Else {      }      [Self.view setneedslayout];      } Completion:nil];  

Sizeforchildcontentcontainer: A container Viewcontroller can use this method to set the size of the Childviewcontroller. When the container viewcontrollerviewwilltransitiontosize:withtransitioncoordinator: is called (when we override this method to call super), The Sizeforchildcontentcontainer method will be called. Then we can send the size that we need to set to Childviewcontroller. When we set this size to the size of the current Childviewcontroller, then the Childviewcontroller Viewwilltransitiontosize method will not be called.

Sizeforchildcontentcontainer the default implementation is to return parentsize.

Systemlayoutfittingsizedidchangeforchildcontentcontainer: This method is called when the following conditions are met:

The current Viewcontroller does not use the AutoLayout layout
The Childrenviewcontroller view uses the AutoLayout layout
Childrenviewcontroller view-systemlayoutsizefittingsize: Change in the value returned by the method (view varies by content, size also changes)

Preferredcontentsize

// From uicontentcontainer Protocol   ReadOnly ) cgsize preferredcontentsize Ns_available_ios (8_0);   -(void) Preferredcontentsizedidchangeforchildcontentcontainer: (ID <UIContentContainer> ) Container Ns_available_ios (8_0);   // From Uiviewcontroller  

Preferredcontentsize is read-only in the Uicontentcontainer protocol, and the corresponding Uiviewcontroller has a writable version. We can use Preferredcontentsize to set the interface size of the childviewcontroller we expect. For example, if the size of the popover used in the application changes, we can use contentsizeforviewinpopover to adjust it before iOS7. IOS7 start this API is deprecated, we can use Preferredcontentsize to set.

When this value of the childviewcontroller of a container viewcontroller is changed, Uikit will call Preferredcontentsizedidchangeforchildcontentcontainer This method tells the current container viewcontroller. We can adjust the interface according to the new size in this method.

Summary
Uiviewcontroller so far (IOS 8.1), the biggest change in the API about layouts is the two sets of protocols that are added in IOS8: Uitraitenvironment and Uicontentcontainer. We can implement these protocols through the demo in our study to observe the time when these methods are finally called in Viewcontroller.

UIView and Uiviewcontroller new related APIs since AutoLayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.