简介
继承:UIResponder:NSObject
协议:NSObject, UITraitEnvironment, NSCoding,
UIDynamicItem, UIAppearanceContainer, UICoordinateSpace,
UIAppearance
创建视图一系列的事
添加子视图方法:
addSubview:
insertSubview:belowSubview:
insertSubview:aboveSubview:
exchangeSubviewAtIndex:withSubviewAtIndex:
当创建了一个视图后,得给视图属性autorisizingMask设置一个值,默认为UIViewAutoresizingNone,视图resizing一般发生在界面横竖屏切换时,但在调用setNeedsLayout 方法时会强制更新其layout
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
动画
在iOS 4 及以后的版本,推荐使用基于块的动画方法
(另外一种时begin/commit animation方法)
视图以下的一个属性改变便可以产生动画
frame bounds centre transform alpha backgroundColor contentStretch
类方法:
- animateWithDuration:animations:
- animateWithDuration: animations:completion:
Methods to Override
Initialisation:
- initWithFrame - (也可自定义初始化方法)
- initWithCoder: -当从nib 文件中加载&视图需自定义
Drawing & printing:
- drawRect: - 需自定义,重绘视图时才重写
Constraints:
- updateConstraints - 需给子视图添加约束
Layout:
- layoutSubviews (常用)
didAddSubview:, willRemoveSubview:
willMoveToSuperview:, didMoveToSuperview
willMoveToWindow:, didMoveToWindow
注意
- 操作程序UI必须放在主线程,除了创建视图本身时没那么严格。
具体属性方法
外观
@property(nonatomic, copy) UIColor *backgroundColor
@property(nonatomic, getter=isHidden) BOOL hidden
@property(nonatomic) CGFloat alpha
@property(nonatomic, getter=isOpaque) BOOL opaque
@property(nonatomic) BOOL clipsToBounds(默认为NO)
@property(nonatomic, readonly, strong) CALayer *layer
( The view is the layer’s delegate)
@property(nonatomic, strong) UIColor *tintColor
(iOS 7新增,这个属性定义了一个非默认的着色颜色值,其值的设置会影响到以视图为根视图的整个视图层次结构。它主要是应用到诸如app图标、导航栏、按钮等一些控件上,以获取一些有意思的视觉效果)
@property(nonatomic, strong) UIView *maskView
(iOS 8 新增,遮罩)
+ layerClass
事件
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
@property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled
@property(nonatomic, getter=isExclusiveTouch) BOOL exclusiveTouch
( If you want this view to handle multi-touch events exclusively, set the values of both multipleTouchEnabled property and the exclusiveTouch property to YES)
大小位置
@property(nonatomic) CGRect frame
@property(nonatomic) CGRect bounds
@property(nonatomic) CGPoint center
@property(nonatomic) CGAffineTransform transform
(Changes to this property can be animated. Use the beginAnimations:context: class method to begin and the commitAnimations class method to end an animation block)
(当改变frame值,不会掉用drawRect方法,如需掉用,把contentMode属性值设为 UIViewContentModeRedraw)
层次结构
@property(nonatomic, readonly) UIView *superview
@property(nonatomic, readonly, copy) NSArray <__kindof UIView *> *subviews
@property(nonatomic, readonly) UIWindow *window
(没有加到窗口时为nil)
- -(void)bringSubviewToFront:(UIView *)view
-(void)sendSubviewToBack:(UIView *)view
-(void)removeFromSuperview(如果父视图为nil时,释放该视图,不可在视图的drawRect方法中调用)
重绘
@property(nonatomic) UIViewAutoresizing autoresizingMask
@property(nonatomic) BOOL autoresizesSubviews
(The default value is YES)
@property(nonatomic) UIViewContentMode contentMode
(默认为 UIViewContentModeScaleToFill)
(contentStretch在iOS 6后废除。 For stretching image-based content, it is simpler to use a UIImageView object with a stretchable image instead of setting this property. You can create a stretchable image using the stretchableImageWithLeftCapWidth:topCapHeight: method of UIImage)
- (CGSize)sizeThatFits:(CGSize)size算出最合适的尺寸,但不立即更新视图尺寸(size:The size for which the view should calculate its best-fitting size 常用CGSizeZero)
-sizeToFit (更新视图,不能重写)
Laying out Subviews
- layoutSubviews
-setNeedsLayout
-layoutIfNeeded(立即更新视图,调用layoutSubviews)
约束管理
@property(nonatomic, readonly) NSArray <__kindof NSLayoutConstraint *> *constraints
-(void)addConstraint:(NSLayoutConstraint *)constraint
-(void)addConstraints:(NSArray <__kindofNSLayoutConstraint > ) constraints
- (void)removeConstraint:(NSLayoutConstraint *)constraint
-(void)removeConstraints:(NSArray<__kindofNSLayoutConstraint > )constraints
未完待续…