iOS开发中的一些小细节(1)
1 去除UITableView 中Section 的Header的粘性(sticky)
1).UITableView的Style默认是Plain,换成Grouped可以解决这个问题,但是会引入一些新的UI问题(每个Section都默认带有Header和Footer等等)。不推荐这种做法。
2).重写UIScrollViewDelegate 中的 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = SECTION_HEADER_HEIGHT;// 这个值需要设置 if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); } }
2.设置view的边宽度,颜色,圆角等。
view.layer.masksToBounds = YES; view.layer.borderWidth = 1; view.layer.borderColor = [UIColor redColor].CGColor;//是CGColor而不是UIColor,注意转换 view.layer.cornerRadius = 3;//如果要设置成圆形(例如圆形图片),讲值设置成view.frame.size.width/2即可
3.判断地址定位是否打开
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) //这个状态是没有打开 { return; }
4.NSTimer的应用(定时行为)
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerTest:) userInfo:@"this is a timer test"//userInfo in this place is a 'id' type.so you should verify in selector; repeats:YES];
-(void)timerTest:(NSTimer *)timer { NSString *message = timer.userInfo; if(![message isKindOfClass:[NSString class]]) { return; } NSLog(@"useInfo is : %@",message); }
一般来说,NSTimer是在viewWillAppear或者某些事件点击后才开始运作的。如果需要停止的话,可以生成一个NSTimer的实例。
NSTimer可能会被重复加载(在cell的重用,之类的),可以用一个属性去控制。
5.根据label的内容改变其高度
//最后得出的比较有用的值是frameOfLabel.size.height //在使用这个值确定label的时候。label.numberOfLines 要设为0 CGRect frameOfLabel = [contentString boundingRectWithSize:CGSizeMake(200 , 50) //设置label的最大size,默认先把行撑满后,再改变高度 options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}//类似NSAttributeString的设置 context:nil];
6.scrollView 在xib 或者storyboard里的约束添加
在scrollview对最外层的view进行四个边的约束 and 对内部的view进行4个边的距离约束之后,仍然会报错。
导致这个问题的原因是,scrollview并不知道其滚动的范围是多少。
在对内部的view 加上宽和高的约束之后。约束变成蓝色。界面可以滚动。
特别的,有的时候,内部view会被压缩几个像素,可以采用设定水平居中的方式解决。
6.
只要navigationController中的任意一层的ViewController调用
[viewControllerInstance dismissViewControllerAnimated:YES completion:nil];
则整个navigationController消失
*************
未完待续
*************