What is Size classes
IOS 8 Adds a new feature to the visual design of the application interface-size Classes, for any device, the width and height of the interface are divided into two descriptions: 正常
and 紧凑
. This allows developers to ignore the specific dimensions of the device and adapt the two classes and their combinations. In this way, both at design time and in code, we can no longer be limited to the specific dimensions, but instead become the visual senses that follow the dimensions to fit. 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