![iOS 8案例开发大全](https://wfqqreader-1252317822.image.myqcloud.com/cover/844/22652844/b_22652844.jpg)
上QQ阅读APP看书,第一时间看更新
实例013 隐藏指定的UIView区域
![](https://epubservercos.yuewen.com/B39025/11229161603800706/epubprivate/OEBPS/Images/figure_0035_0002.jpg?sign=1739297670-ZEs1tY0jroo9wRbS7ZkRqV1bhcgA3ayV-0-1b90cc5858689f2e6ccc3ef51f985ca1)
实例说明
本实例的功能是使用UIView的属性hidden来隐藏指定的区域。当属性hidden的值为YES时隐藏UIView,当属性hidden的值为NO时显示UIView。当单击本实例中的“点击”按钮时,会实现文本隐藏和文本显示的转换。
具体实现
实例文件UIkitPrjHide.h的实现代码如下所示。
#import "SampleBaseController.h" @interface UIKitPrjHide : SampleBaseController { @private UILabel* label_; } @end
实例文件UIkitPrjHide.m的实现代码如下所示。
#import "UIKitPrjHide.h" @implementation UIKitPrjHide #pragma mark ----- Override Methods ----- // finalize - (void)dealloc { [label_ release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; label_ = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 320, 200 )]; label_.textAlignment = UITextAlignmentCenter; label_.backgroundColor = [UIColor blackColor]; label_.textColor = [UIColor whiteColor]; label_.text = @"I'm here! "; [self.view addSubview:label_]; UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake( 0, 0, 100, 40 ); CGPoint newPoint = self.view.center; newPoint.y = self.view.frame.size.height -70; button.center = newPoint; [button setTitle:@"点击" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControl EventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonDidPush { label_.hidden = ! label_.hidden; } - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { [self.navigationController setNavigationBarHidden:NO animated:YES]; } @end
执行后的效果如图2-16所示,按下图2-16中的“点击”按钮后会隐藏文本,如图2-17所示。
![](https://epubservercos.yuewen.com/B39025/11229161603800706/epubprivate/OEBPS/Images/figure_0036_0001.jpg?sign=1739297670-zhy6Hf1ViG7VOZsYIGzFmEo5jNnPRkL2-0-474612a0d93b59db59035cdacfe8a100)
图2-16 执行效果
![](https://epubservercos.yuewen.com/B39025/11229161603800706/epubprivate/OEBPS/Images/figure_0036_0002.jpg?sign=1739297670-FmIRf4k6FTlw4U00iO1fRo1xGNhfAXyK-0-824b91a0819c9c5aedf18bacbc528652)
图2-17 隐藏了文本