我想将一个视图移动到另一个视图的顶部,我如何知道视图的z索引,以及如何移动到顶部?
发布于 2011-01-12 06:42:11
UIView
同级按其添加到其superview的顺序进行堆叠。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;
同级视图在subviews
数组中从后到前排序。因此,最上面的视图将是:
[parentView.subviews lastObject];
底部视图为:
[parentView.subviews objectAtIndex:0];
正如Kolin所说,[parentView bringSubviewToFront:view]
将把视图带到顶部,但只有当视图都是层次结构中的兄弟时才会出现这种情况。
发布于 2011-01-08 09:49:56
您可以使用视图的层(它是一个CALayer
对象)的zPosition
属性来更改视图的z索引。
theView.layer.zPosition = 1;
正如Viktor Nordling补充的那样,“大的值在上面。你可以使用任何你想要的值,包括负值。”默认值为0。
您需要导入QuartzCore框架才能访问该层。只需将这行代码添加到您的实现文件的顶部。
#import "QuartzCore/QuartzCore.h"
发布于 2011-01-08 09:54:43
[parentView bringSubviewToFront:view] ;
https://stackoverflow.com/questions/4631878
复制相似问题