program story

UIView 클래스에서 addSubview와 insertSubview의 차이점

inputbox 2020. 9. 21. 07:40
반응형

UIView 클래스에서 addSubview와 insertSubview의 차이점


프로그래밍 방식으로 뷰를 추가 할 때 addSubviewinsertSubView메서드 의 차이점은 무엇입니까 ?


유일한 차이점은 뷰가 추가되는 위치입니다. 맨 앞 뷰인지 ( addSubview:), 5 번째 하위 뷰 이전 insertSubview:atIndex:인지 ( ) 또는 다른 하위 뷰 바로 뒤에 있는지 ( insertSubview:aboveSubview:).


를 사용하여 insertSubView:뷰의 z 순서를 결정하는 인덱스를 지정할 수 있습니다. 인덱스가 높은 뷰는 인덱스가 낮은 뷰 위에 있습니다.


차이가 없다고 생각합니다. addSubview:간단하고 편리한 방법입니다.

[view insertSubview:aView atIndex:[view.subviews count]]

1. addSubview 배열에 하위보기를 추가 한 다음보기의 레이어에 추가

- (void)addSubview:(UIView *)subview
{
    [_subviews addObject:subview];
    [_layer addSublayer:subview.layer];
}

}

2. insertSubview 동안 뷰를 하위 뷰로 추가 한 다음 [_layer insertSublayer:subview.layer atIndex:index];

- (void)insertSubview:(UIView *)subview atIndex:(NSInteger)index
{
  [self addSubview:subview];
  [_layer insertSublayer:subview.layer atIndex:index];
}

참고 URL : https://stackoverflow.com/questions/1519141/difference-between-addsubview-and-insertsubview-in-uiview-class

반응형