About Views

About Views
IMPORTANT
This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Views are the building blocks for constructing your user interface. Rather than using one view to present your content, you are more likely to use several views, ranging from simple buttons and text labels to more complex views such as table views, picker views, and scroll views. Each view represents a particular portion of your user interface and is generally optimized for a specific type of content. By building view upon view, you get a view hierarchy.

image: ../Art/windowlayers_world_clock.pdf
image: ../Art/windowlayers_world_clock.pdf

Purpose. Views allow users to:
Experience app content

Navigate within an app

Implementation. Views are implemented in the UIView
class and discussed in UIView Class Reference.
Configuration. Configure views in Interface Builder, in the View section of the Attributes Inspector. A few configurations cannot be made through the Attributes Inspector, so you must make them programmatically. You can set other configurations programmatically, too, if you prefer.

image: ../Art/uiview_attributes_inspector_plain_2x.png
image: ../Art/uiview_attributes_inspector_plain_2x.png

Content of Views
All views in UIKit are subclasses of the base class UIView
. For example, UIKit has views specifically for presenting images, text, and other types of content. In places where the predefined views do not provide what you need, you can also define custom views and manage the drawing and event handling yourself.
Use the Mode (contentMode
) field to specify how a view lays out its content when its bounds change. This property is often used to implement resizable controls. Instead of redrawing the contents of the view every time its bounds change, you can use this property to specify that you want to scale the contents or pin them to a particular spot on the view.
The Tag (tag
) field serves as an integer that you can use to identify view objects in your app.

image: ../Art/uiview_attributes_inspector_group-layout_tagging.pdf
image: ../Art/uiview_attributes_inspector_group-layout_tagging.pdf

Behavior of Views
By default, the User Interaction Enabled (userInteractionEnabled
) checkbox is selected, which means that user events—such as touch and keyboard—are delivered to the view normally. When the checkbox is unselected, events intended for the view are ignored and removed from the event queue.
The Multiple Touch (multipleTouchEnabled
) checkbox is unselected by default, which means that the view receives only the first touch event in a multi-touch sequence. When selected, the view receives all touches associated with a multitouch sequence.

image: ../Art/uiview_attributes_inspector_group-events.pdf
image: ../Art/uiview_attributes_inspector_group-events.pdf

Views have a number of properties related to drawing behavior:
image: ../Art/uiview_attributes_inspector_group-drawing_sizing.pdf
image: ../Art/uiview_attributes_inspector_group-drawing_sizing.pdf

The Opaque (opaque
) checkbox tells the drawing system how it should treat the view. If selected, the drawing system treats the view as fully opaque, which allows the drawing system to optimize some drawing operations and improve performance. If unselected, the drawing system composites the view normally with other content. You should always disable this checkbox if your view is fully or partially transparent.

If the Hidden (hidden
) checkbox is selected, the view disappears from its window and does not receive input events.

When the Clears Graphics Context (clearsContextBeforeDrawing
) checkbox is selected, the drawing buffer is automatically cleared to transparent black before the view is drawn. This behavior ensures that there are no visual artifacts left over when the view’s contents are redrawn.

Selecting the Clip Subviews (clipsToBounds
) checkbox causes subviews to be clipped to the bounds of the view. If unselected, subviews whose frames extend beyond the visible bounds of the view are not clipped.

When the Autoresize Subviews (autoresizesSubviews
) checkbox is selected, the view adjusts the size of its subviews when its bounds change.

Appearance of Views
Background Color and Alpha
Adjusting the Alpha (alpha
) field changes the transparency of the view as a whole. This value can range from0.0
(transparent) to 1.0
(opaque). Setting a view’s alpha value does not have an effect on embedded subviews.
Use the Background (backgroundColor
) field to select a color to fill in the entire frame of the view. The background color appears underneath all other content in the view.

image: ../Art/uiview_attributes_inspector_group-appearance.pdf
image: ../Art/uiview_attributes_inspector_group-appearance.pdf

Appearance Proxies
You can use an appearance proxy to set particular appearance properties for all instances of a view in your application. For example, if you want all sliders in your app to have a particular minimum track tint color, you can specify this with a single message to the slider’s appearance proxy.
There are two ways to customize appearance for objects: for all instances and for instances contained within an instance of a container class.
To customize the appearance of all instances of a class, use appearance
to get the appearance proxy for the class.
[[UISlider appearance] setMinimumTrackTintColor:[UIColor greenColor]];

To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, you use appearanceWhenContainedIn:
to get the appearance proxy for the class.
[[UISlider appearanceWhenContainedIn:[UIView class], nil]

setMinimumTrackTintColor:[UIColor greenColor]];

NOTE
You cannot use the appearance proxy with the tintColor
property on UIView
. For more information on using tintColor
, see Tint Color.

Tint Color
Views have a tintColor
property that specifies the color of key elements within the view. Each subclass ofUIView
defines its own appearance and behavior for tintColor
. For example, this property determines the color of the outline, divider, and icons on a stepper:

image: ../Art/uistepper_intro.png
image: ../Art/uistepper_intro.png

The tintColor
property is a quick and simple way to skin your app with a custom color. Setting a tint color for a view automatically sets that tint color for all of its subviews. However, you can manually override this property for any of those subviews and its descendants. In other words, each view inherits its superview’s tint color if its own tint color is nil
. If the highest level view in the view hierarchy has a nil
value for tint color, it defaults to the system blue color.

Template Images
In iOS 7, you can choose to treat any of the images in your app as a template—or stencil—image. When you elect to treat an image as a template, the system ignores the image’s color information and creates an image stencil based on the alpha values in the image (parts of the image with an alpha value of less than 1.0
get treated as completely transparent). This stencil can then be recolored using tintColor
to match the rest of your user interface.
By default, an image (UIImage
) is created with UIImageRenderingModeAutomatic
. If you haveUIImageRenderingModeAutomatic
set on your image, it will be treated as template or original based on its context. Certain UIKit elements—including navigation bars, tab bars, toolbars, segmented controls—automatically treat their foreground images as templates, although their background images are treated as original. Other elements—such as image views and web views—treat their images as originals. If you want your image to always be treated as a template regardless of context, set UIImageRenderingModeAlwaysTemplate
; if you want your image to always be treated as original, set UIImageRenderingModeAlwaysOriginal
.
To specify the rendering mode of an image, first create a standard image, and then call theimageWithRenderingMode:
method on that image.
UIImage *myImage = [UIImage imageNamed:@"myImageFile.png"];

myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Using Auto Layout with Views
The Auto Layout system allows you to define layout constraints for user interface elements, such as views and controls. Constraints represent relationships between user interface elements. You can create Auto Layout constraints by selecting the appropriate element or group of elements and selecting an option from the menu in the bottom right corner of Xcode’s Interface Builder.
Auto layout contains two menus of constraints: pin and align. The Pin menu allows you to specify constraints that define some relationship based on a particular value or range of values. Some apply to the control itself (width) while others define relationships between elements (horizontal spacing). The following tables describes what each group of constraints in the Auto Layout menu accomplishes:

Constraint Name

Purpose

image: ../Art/xcode_accessibility_dimensions.png
image: ../Art/xcode_accessibility_dimensions.png

Sets the width or height of a single element.

image: ../Art/xcode_accessibility_spacing.png
image: ../Art/xcode_accessibility_spacing.png

Sets the horizontal or vertical spacing between exactly two elements.

image: ../Art/xcode_accessibility_space_to_superview.png
image: ../Art/xcode_accessibility_space_to_superview.png

Sets the spacing from one or more elements to the leading, trailing, top, or bottom of their container view. Leading and trailing are the same as left and right in English, but the UI flips when localized in a right-to-left environment.

image: ../Art/xcode_accessibility_equally.png
image: ../Art/xcode_accessibility_equally.png

Sets the widths or heights of two or more elements equal to each other.

image: ../Art/xcode_accessibility_edges.png
image: ../Art/xcode_accessibility_edges.png

Aligns the left, right, top, or bottom edges of two or more elements.

image: ../Art/xcode_accessibility_centers.png
image: ../Art/xcode_accessibility_centers.png

Aligns two or more elements according to their horizontal centers, vertical centers, or bottom baselines. Note that baselines are different from bottom edges. These values may not be defined for certain elements.

image: ../Art/xcode_accessibility_center_in_container.png
image: ../Art/xcode_accessibility_center_in_container.png

Aligns the horizontal or vertical centers of one or more elements with the horizontal or vertical center of their container view.

The “Constant” value specified for any Pin constraints (besides Widths/Heights Equally) can be part of a “Relation.” That is, you can specify whether you want the value of that constraint to be equal to, less than or equal to, or greater than or equal to the value.

image: ../Art/xcode_auto_layout_attributes_inspector.png
image: ../Art/xcode_auto_layout_attributes_inspector.png

For more information, see Auto Layout Guide.

Making Views Accessible
To enhance the accessibility information for an item, select the object on the storyboard and open the Accessibility section of the Identity inspector.
For more information, see Accessibility Programming Guide for iOS.

Debugging Views
When debugging issues with views, watch for this common pitfall:
Setting conflicting opacity settings. You should not set the opaque
property to YES
if your view has an alpha value of less than 1.0
.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末装黑,一起剝皮案震驚了整個濱河市背桐,隨后出現的幾起案子账阻,更是在濱河造成了極大的恐慌配深,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異高镐,居然都是意外死亡目养,警方通過查閱死者的電腦和手機救军,發(fā)現死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來倘零,“玉大人唱遭,你說我怎么就攤上這事〕适唬” “怎么了拷泽?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長袖瞻。 經常有香客問我司致,道長,這世上最難降的妖魔是什么聋迎? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任脂矫,我火速辦了婚禮,結果婚禮上霉晕,老公的妹妹穿的比我還像新娘庭再。我一直安慰自己,他們只是感情好牺堰,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布拄轻。 她就那樣靜靜地躺著,像睡著了一般萌焰。 火紅的嫁衣襯著肌膚如雪哺眯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天扒俯,我揣著相機與錄音奶卓,去河邊找鬼。 笑死撼玄,一個胖子當著我的面吹牛夺姑,可吹牛的內容都是我干的。 我是一名探鬼主播掌猛,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼盏浙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了荔茬?” 一聲冷哼從身側響起废膘,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎慕蔚,沒想到半個月后丐黄,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡孔飒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年灌闺,在試婚紗的時候發(fā)現自己被綠了艰争。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡桂对,死狀恐怖甩卓,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情蕉斜,我是刑警寧澤逾柿,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站蛛勉,受9級特大地震影響鹿寻,放射性物質發(fā)生泄漏。R本人自食惡果不足惜诽凌,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望坦敌。 院中可真熱鬧侣诵,春花似錦、人聲如沸狱窘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蘸炸。三九已至躬络,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間搭儒,已是汗流浹背穷当。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留淹禾,地道東北人馁菜。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像铃岔,于是被迫代替她去往敵國和親汪疮。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,429評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,441評論 5 6
  • 下周要到長沙毁习, 現在就在車上智嚷。 離開家中寶寶, 實屬無奈之舉纺且。 托管武術鋼琴盏道, 考試比賽作業(yè), 鼻涕咳嗽感冒隆檀, 降...
    奔跑的馬齒莧閱讀 134評論 0 0
  • 昨天去補課班兼職的時候被那里的孩子們說我像《歡樂頌》里的關關摇天,還有幾個調皮的小朋友直接叫我關關老師粹湃,真的覺得我應...
    林夕焉閱讀 117評論 0 0
  • 晚秋橙葉滿林廊, 風吹彩蝶送芬芳泉坐。 綠垢霾藍皆晦暗为鳄, 天姿國色數紅黃。 ...
    A都督閱讀 218評論 0 0