我想把一个视图移到另一个视图的上方,我怎么知道视图的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]将把视图带到顶部,但只有当视图在层次结构中都是兄弟姐妹时才会出现这种情况。
其他回答
我们可以在ios中使用zPosition
如果我们有一个名为salonDetailView的视图 例如:@IBOutlet弱var salonDetailView: UIView!
以我为例,请看图片
并有GMSMapView的UIView @IBOutlet weak var mapViewUI: GMSMapView!
在mapViewUI上显示View salonDetailView
使用zPosition如下所示
salonDetailView.layer.zPosition = 1
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]将把视图带到顶部,但只有当视图在层次结构中都是兄弟姐妹时才会出现这种情况。
在你想要放到顶部的视图中… (迅速)
superview?.bringSubviewToFront(self)
[parentView bringSubviewToFront:view] ;
如果你想通过XCode的Interface Builder来实现,你可以使用Editor->Arrangement下的菜单选项。在那里你会发现“发送到前面”,“发送到后面”,等等。
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 如何使用Xcode创建。ipa文件?