In IOS8, the new size classes feature, which is an abstraction of all of the current iOS device sizes, is also an abstraction, think about how many iOS-sized devices you have now: Iphone4-5-6-6plus, ipad, ipad Mini, IWatch, It must be a terrible thing to write a different layout for a particular device, as before.
Now, with the sizeclass, things are better: you are not a lot of equipment, then we will only divide the width and height of the screen into three cases: (compact, Regular, any), that is, compact, normal and arbitrary. This width and height of 331 integration, altogether 9 of the situation. As shown, for each case, we can set the UIView automatic layout constraint individually in storyboard or xib, or even if a button is displayed, if necessary.
In contrast to the responder chain in UIKit, Traitcollection will be delivered from top to bottom in view hierarchy. For a UI part that does not specify Traitcollection, the traitcollection of its parent node is used. This can be quite useful when the layout contains the Childviewcontroller interface. Another very useful uitraitenvironment in this interface is-traitcollectiondidchange:. This method will be called when the traitcollection is changed. In practice, we tend to rewrite-traitcollectiondidchange in Viewcontroller: or-willtransitiontotraitcollection: Withtransitioncoordinator: Method (for Viewcontroller, the latter may be a better choice because the transition context is provided for easy animation, but for normal View there is only one way), The current traitcollection is then judged and re-laid and animated. The code will look something like this:
- -(void) Willtransitiontotraitcollection: (uitraitcollection *) newcollection
- Withtransitioncoordinator: (ID <UIViewControllerTransitionCoordinator>) Coordinator
- {
- [Super Willtransitiontotraitcollection:newcollection
- Withtransitioncoordinator:coordinator];
- [Coordinator animatealongsidetransition:^ (ID <UIViewControllerTransitionCoordinatorContext> context) {
- if (Newcollection.verticalsizeclass = = uiuserinterfacesizeclasscompact) {
- //to do:modify Something for compact vertical size
- } Else {
- //to do:modify Something for other vertical size
- }
- [Self.view Setneedslayout];
- } Completion:nil];
- }
In two to do, we should delete or add or change the Auto Layout constraints under different conditions (of course, you can do anything else you want), and then call-setneedslayout to trigger the transfer animation in context. If you stick with the code, you may have to face the problem of removing the old constraints and adding new constraints for different Size Classes, which can be a lot of trouble (at least I think it's going to be a problem). But if we use IB, these things and code can be omitted, we can very easily specify the various size Classes constraints in IB (we will show how to use IB to correspond to size Classes). In addition to the use of IB can not only save hundreds of lines of layout code, but also from the new Xcode and IB to get a lot of design can be real-time monitoring, viewing and debugging features. It can be said that the time-consuming and cost gap between the handwritten UI and the IB design has been further widened, and many of the handwriting UI has not been implemented, but IB can do it without hesitation. In this sense, the new IB and Size Classes system can be said mercilessly to the handwritten code sentenced to a reprieve. In addition, the introduction of new APIs and systems also sentenced the death penalty to many of our familiar uiviewcontroller of rotating old friends, such as the following API deprecated:
- @property (nonatomic, readonly) uiinterfaceorientation interfaceorientation
- -Willrotatetointerfaceorientation:duration:
- -Willanimaterotationtointerfaceorientation:duration:
- -Didrotatefrominterfaceorientation:
- -Shouldautomaticallyforwardrotationmethods
Now all is unified to the Viewwilltransitiontosize:withtransitioncoordinator: the concept of rotation is no longer advocated for use. In fact, think about, the so-called rotation, but it is a change of Size, we have been Apple for many years, is not it?
The specific embodiment in Xcode is as follows:
But we see that the width and height of the graph are all Any
, what does any mean? If weight
set to Any
, height
then the Regular
interface element in that state will exist in the state as long as it is height
Regular
present, both in and out weight
Regular
Compact
. This relationship should be called an inheritance relationship, and the specific four interface descriptions and inheritable interfaces are described below:
- W:compact h:compact Inheritance (w:any h:compact , w:compact h:any , w:any h:any)
- W:regular h:compact Inheritance (w:any h:compact , w:regular h:any , w:any h:any)
- W:compact h:regular Inheritance (w:any h:regular , w:compact h:any , w:any h:any)
- W:regular h:regular Inheritance (w:any h:regular , w:regular h:any , w:any h:any)
We know that iOS 8 the device interface below can be described as 4, but what exactly is the description of so many devices (Iphone4s,iphone5/5s,iphone6,iphone6 plus,ipad,apple Watch)? After reviewing official documentation and specific practices, the specific correspondence is as follows:
- Iphone4s,iphone5/5s,iphone6
- Vertical screen: (w:compact h:regular)
- Horizontal screen: (w:compact h:compact)
- IPhone6 Plus
- Vertical screen: (w:compact h:regular)
- Horizontal screen: (w:regular h:compact)
- Ipad
- Vertical screen: (w:regular h:regular)
- Horizontal screen: (w:regular h:regular)
- Apple Watch (guess)
- Vertical screen: (w:compact h:compact)
- Horizontal screen: (w:compact h:compact)
Size Classes Handwriting Code
In order to characterize Size Classes
, Apple introduced a new class in IOS8, uitraitcollection
. This class encapsulates information such as the size class in horizontal and vertical directions. Most of the basic classes of UI in IOS8 Uikit, including Uiscreen,uiwindow,uiviewcontroller and UIView, implement the uitraitenvironment
interface, through which the traitcollection
This property, we can get the corresponding uitraitcollection
object, so that we know the current size Class, and further determine the layout of the interface. In contrast to the responder chain in Uikit, traitcollection
will be passed from top to bottom in view hierarchy
. For UI parts that do not have a traitcollection
specified, the traitcollection
of their parent node is used. This is useful when the layout contains an interface for Childviewcontroller
. Another very useful one in the uitraitenvironment
interface is -traitcollectiondidchange:
. This method is called when traitcollection
is changed. In practice, we tend to override -traitcollectiondidchange in viewcontroller
:
or - Willtransitiontotraitcollection:withtransitioncoordinator:
method (for Viewcontroller
, The latter may be a better choice because it provides a convenient way to animate a transition context, but for a normal view there is only one previous method, and then the current traitcollection
is judged and re-laid and animated. The code will look something like this:
123456789 |
-(void) Willtransitiontotraitcollection: (uitraitcollection *) newcollectionwithtransitioncoordinator: (ID <UIViewControllerTransitionCoordinator>) Coordinator{ [sup ER willtransitiontotraitcollection:newcollection withtransitioncoordinator:coordinator]; [Coordinator animatealongsidetransition:^ (ID <UIViewControllerTransitionCoordinatorContext> context) { if (Newcollection.verticalsizeclass = = uiuserinterfacesizeclasscompact) { //to do:modify something for com Pact vertical Size } else { //to do:modify something for other vertical size } [Self.view setneedslayou T]; } Completion:nil]; }
|
In two to do, we want the handwritten code to be adjusted for different states.
Size Classes and Interface Builder
Xcode6 Interface Builder
Size Class
has strong support, Xib can open size classes such as:
Size Classes
under different descriptions, the interface elements can be installed or not installed, the specific operation
Size Classes with Image Asset
Xcode6 Image Asset
also supports Size Class
, that is to say, we can Size Class
specify different images for different. In Image Asset
the edit panel to select a picture, Inspector now has a Width
combination of one and Height
, add we need to correspond Size Class
, and then drag the appropriate diagram up, so that at run time SDK
will be selected from the corresponding Size
diagram to replace. Size Class
the supported Image Asset
editing effects are as follows:
IOS8 the understanding and use of Size classes