我想将一个视图移到另一个视图之上,我怎么知道视图的 z 索引,以及如何移到顶部?
UIView兄弟姐妹按照它们添加到其父视图的顺序堆叠。层次结构方法和UIView属性用于管理视图顺序。在 UIView.h 中:
UIView
@property(nonatomic,readonly) UIView *superview; @property(nonatomic,readonly,copy) NSArray *subviews; - (void)removeFromSuperview; - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; - (void)addSubview:(UIView *)view; - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; - (void)bringSubviewToFront:(UIView *)view; - (void)sendSubviewToBack:(UIView *)view;
兄弟视图在数组中从后到前subviews排列。所以最顶层的视图将是:
subviews
[parentView.subviews lastObject];
底部视图将是:
[parentView.subviews objectAtIndex:0];
就像 Kolin Krewinkel 所说的那样,[parentView bringSubviewToFront:view]会将视图带到顶部,但这仅适用于视图都是层次结构中的兄弟姐妹的情况。
[parentView bringSubviewToFront:view]