UIView Summary

1 Life Cycle

  1. UIView no designated initialization method.
  2. initialization

3. Life Cycle

updateConstraintsAnd layoutSubviewsthe relationship a bit like collecting - the implementation of the relationship. (Preparing the data, use data)

Trigger layoutSubviews way:

  1. setFrame, and the frame is not CGRectZero, (and before and after the change)
  2. addSubview:,removeSubview:
  3. Scroll UIScrollView
  4. Screen Rotation
  5. setNeedsLayout, setNeedUpdateConstraints (and the changes before and after)
  6. Change the frame, only to trigger their layoutSubviews, will not affect the parent view, view of brothers

2 code organization

  1. initialize the coordinate calculation, using the static variable calculation only once

  2. initWithFrame: in

    Assembly subview

  3. setModel in

    Configuration data source, needLayout

  4. layoutSubviews中

    Process layout

Note that some third-party libraries of view, may not be implemented layoutSubviews, but rewrote setFrame :, if we take them as subview View, and pay attention to:

3 Precautions

3.1 Draw Order

Depth-first: the parent view, under the brothers view of the situation, the parent view drawn first, then draw the child views the parent view, drawn last Brothers view.

4 Common API

4.1 Getting controller

//比superview准确,用superview的话,要判断navigationController和tabbarController
- (UIViewController *)viewController {
    UIResponder *responder = self;
    while (![responder isKindOfClass:[UIViewController class]]) {
        responder = [responder nextResponder];
        if (nil == responder) {
            break;
        }
    }
    return (UIViewController *)responder;
}
复制代码

4.2 Some Properties

4.2.1 Transparency related

//1. alpha,hidden。较少用,opaque。
//2. opacity,layer的属性,相当于view.alpha,但设置为0,也不影响事件响应。
复制代码

Above will affect the applet (or sub-Layer), if desired parent view transparent, opaque subview.

//只对颜色有效,对图片无效
superView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5];
复制代码

4.2.2 Trimming related

//效果上没区别
view.clipsToBounds = YES;
layer.masksToBounds = YES;
复制代码

4.2.3 Other

//1. sizeToFit,在autoLayout中无效了,要强行生效,可以写在viewDidLayoutSubviews中

//2. tintColor,默认背景色,会被子类继承

//3. multipleTouchEnabled,是否开启多点触摸,默认NO

//4. exclusiveTouch,事件独占,默认NO
表示事件独占,如果UIView是事件的firstResponder,那么第一个手指离开前,其他视图不会响应任何触摸事件。(仅Touch事件,不包括手势)
iOS 解决页面按钮同时点击,同时触发问题 设置所有UIButton的ExclusiveTouch属性为YES

-(void)setExclusiveTouchForButtons:(UIView *)myView {
    for (UIView * v in [myView subviews]) {
        if([v isKindOfClass:[UIButton class]])
            [((UIButton *)v) setExclusiveTouch:YES];
        else if ([v isKindOfClass:[UIView class]]){
            [self setExclusiveTouchForButtons:v];
        }
    }
}

//5. 根据tag拿view
viewWithTag:

//6. 是否为子视图
isDescendantOfView:

//7.层级、响应相关
insertSubview:belowSubview:
insertSubview:aboveSubview:

becomeFirstResponder
resignFirstResponder

bringSubviewToFront:
sendSubviewToBack:

//8. sizeToFit,会被autoLayout覆盖(除非在viewDidLayoutSubviews中调用,不推荐),不需要太在意这个属性
sizeToFit    //不要重写,可能会改变原始frame
siztThatFits    //重写这个,为sizeToFit提供数据,"照这个数据fit"

//9. 刷新相关
setNeedsUpdateConstraints
updateConstraintsIfNeeded

setNeedsLayout
layoutIfNeeded

setNeedsDisplay
复制代码

5 Other

5.1 frame and bounds

  • Modify the view's bounds.size, center will not change. origin become.
  • Modify bounds.origin, this view will not change, and change is his subviews. Because equivalent changed frame in which the sub-view coordinate system. UIScrollView uses.

Reproduced in: https: //juejin.im/post/5d077f62f265da1b67210ed2

Guess you like

Origin blog.csdn.net/weixin_34403693/article/details/93180866
Recommended