我想把一个视图移到另一个视图的上方,我怎么知道视图的z索引,以及如何移到顶部?


当前回答

UIView的兄弟姐妹是按照它们被添加到父视图的顺序堆叠的。UIView的层次结构方法和属性是用来管理视图顺序的。在UIView.h:

@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;

兄弟视图在子视图数组中由后往前排序。所以最上面的视图是:

[parentView.subviews lastObject];

底部视图将是:

[parentView.subviews objectAtIndex:0];

就像Kolin Krewinkel说的,[parentView bringSubviewToFront:view]将把视图带到顶部,但只有当视图在层次结构中都是兄弟姐妹时才会出现这种情况。

其他回答

UIView的兄弟姐妹是按照它们被添加到父视图的顺序堆叠的。UIView的层次结构方法和属性是用来管理视图顺序的。在UIView.h:

@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;

兄弟视图在子视图数组中由后往前排序。所以最上面的视图是:

[parentView.subviews lastObject];

底部视图将是:

[parentView.subviews objectAtIndex:0];

就像Kolin Krewinkel说的,[parentView bringSubviewToFront:view]将把视图带到顶部,但只有当视图在层次结构中都是兄弟姐妹时才会出现这种情况。

我们可以在ios中使用zPosition

如果我们有一个名为salonDetailView的视图 例如:@IBOutlet弱var salonDetailView: UIView!

以我为例,请看图片

并有GMSMapView的UIView @IBOutlet weak var mapViewUI: GMSMapView!

在mapViewUI上显示View salonDetailView

使用zPosition如下所示

salonDetailView.layer.zPosition = 1
[parentView bringSubviewToFront:view] ;

或者用这个

- (void)insertSubview:(UIView *)view belowSubview:(UIView*)siblingSubview;

根据这里

在你想要放到顶部的视图中… (迅速)

superview?.bringSubviewToFront(self)