![iOS 8案例开发大全](https://wfqqreader-1252317822.image.myqcloud.com/cover/844/22652844/b_22652844.jpg)
上QQ阅读APP看书,第一时间看更新
实例022 设置视图位置互换显示(1)
![](https://epubservercos.yuewen.com/B39025/11229161603800706/epubprivate/OEBPS/Images/figure_0053_0002.jpg?sign=1739050687-hl6AoNOGqff0aTeSoWGOSeQ6LtiBYxs4-0-314abeaa0a7a20e3c05ee857153ac7db)
实例说明
在iOS应用中,可以使用sendSubviewToFront方法将某个视图放在前方显示,使用sendSubview-ToBack方法将某个视图放在后方显示。
本实例的功能是,设置两个重叠的视图,可以设置其中的一个视图靠前显示还是靠后隐藏显示。本实例首先设置了两个标签代表两块视图区域,然后在下方设置了“bringFront”按钮和“sendBack”按钮。单击这两个按钮后,会实现指定标签块的前置和靠后显示效果。
具体实现
实例文件UIkitPrjSiblings.h的实现代码如下所示。
#import "SampleBaseController.h" @interface UIKitPrjSiblings : SampleBaseController { @private UILabel* labelA_; UILabel* labelB_; } @end
实例文件UIkitPrjSiblings.m的实现代码如下所示。
#import "UIKitPrjSiblings.h" #pragma mark ----- Private Methods Definition ----- @interface UIKitPrjSiblings () - (void)bringFrontDidPush; - (void)sendBackDidPush; @end #pragma mark ----- Start Implementation For Methods ----- @implementation UIKitPrjSiblings // finalize - (void)dealloc { [labelA_ release]; [labelB_ release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; // 追加标签A labelA_ = [[UILabel alloc] initWithFrame:CGRectMake( 10, 10, 150, 150 )]; labelA_.backgroundColor = [UIColor redColor]; labelA_.textAlignment = UITextAlignmentCenter; labelA_.text = @"A"; [self.view addSubview:labelA_]; // 追加标签B labelB_ = [[UILabel alloc] initWithFrame:CGRectMake( 100, 100, 150, 150 )]; labelB_.backgroundColor = [UIColor blueColor]; labelB_.textAlignment = UITextAlignmentCenter; labelB_.text = @"B"; [self.view addSubview:labelB_]; // 追加bringFront按钮 UIButton* bringFrontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; bringFrontButton.frame = CGRectMake( 0, 0, 100, 40 ); CGPoint newPoint = self.view.center; newPoint.x -= 50; newPoint.y = self.view.frame.size.height -100; bringFrontButton.center = newPoint; [bringFrontButton setTitle:@"bringFront" forState:UIControlStateNormal]; [bringFrontButton addTarget:self action:@selector(bringFrontDidPush) forControlEvents:UIControlEventTouchUpInside] // 追加sendBack按钮 UIButton* sendBackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; sendBackButton.frame = CGRectMake( 0, 0, 100, 40 ); newPoint.x += 100; sendBackButton.center = newPoint; [sendBackButton setTitle:@"sendBack" forState:UIControlStateNormal]; [sendBackButton addTarget:self action:@selector(sendBackDidPush) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:bringFrontButton]; [self.view addSubview:sendBackButton]; } #pragma mark ----- Private Methods ----- - (void)bringFrontDidPush { [self.view bringSubviewToFront:labelA_]; } - (void)sendBackDidPush { [self.view sendSubviewToBack:labelA_]; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [self.navigationController setNavigationBarHidden:NO animated:YES]; } @end
执行后的效果如图2-26所示。
![](https://epubservercos.yuewen.com/B39025/11229161603800706/epubprivate/OEBPS/Images/figure_0055_0001.jpg?sign=1739050687-OzzRuMgFjWtNYDpSGWzhLBnDOaCZ7yev-0-8a07b0c7b0a1b10f3a1a8b9736316336)
图2-26 执行效果