iOS开发过程中的奇淫技巧记录


为了修改带分组tableview的section header跟随置顶的问题,网上的奇淫技巧比方通过修改scroller的回调方法,体验不好,正规的方法是修改为tableView的UITableViewStyleGrouped模式,但该模式下列表section Header的高度过高,需要设置一个footer的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

 

结束当前界面,进入新的界面,使用导航的pushViewController进入下一个页面,需要删除navigationController.viewControllers中保存的当前页面

[self.navigationController pushViewController:vc animated:YES];
        
//结束当前界面
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:weakSelf.navigationController.viewControllers];
            [viewControllers removeObjectAtIndex:viewControllers.count - 2];
            weakSelf.navigationController.viewControllers = viewControllers;
});



短延时处理的奇淫技巧:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

});

              

异常处理:

//抛出异常   
[NSException raise:@"WebService error" format:@"%@", returnJson4SOAP];

//捕获异常    
@try 
    {

    }@catch (NSException * e) { 
        NSLog(@"Exception: %@", e); 
        return;
 
    }



约束一个元素的位置,最原始的就是frame,如果是使用约束,也需要至少指定左、上,宽、高,否则约束会出现异常(xib中配置约束或者使用Masonry配置约束),比方设置一个view在屏幕的底部的约束:

[view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view);
                make.bottom.equalTo(self.view);
                make.width.equalTo(@SCREEN_WIDTH);
                make.height.equalTo(@102);
}];


圆角处理:不能仅仅把图片处理成圆角!

imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.masksToBounds = YES;


界面进入后隐藏输入框默认焦点的键盘

推荐做法:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!self.textView.isFirstResponder) {
        [self.textView resignFirstResponder];
    }
}


不推荐的做法,会导致所有的UITextView出不来键盘:

@implementation UITextView (DisableCopyPaste)

- (BOOL)canBecomeFirstResponder
{
    return NO;
}

@end



诸如导航栏标题上的背景颜色修改,搜索框的背景修改,网上都有很多奇淫技巧,发现都不正规,正规的改法往往很简单,原因在于控件有很多层次,由于没有找到背景所在的正确的层级,所以你发现很多的设置不生效,关键的点是经常使用Debug View Hierarchy观察你想要修改的控件对象,这时候修改才会一步到位,比方搜索框的背景修改:

不生效:
    self.searchBar.backgroundColor = UIColorMake(170, 148, 105);
生效的方法: 
    [self.searchBar setBackgroundImage:[UIImage qmui_imageWithColor:UIColorDarkGold] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self.searchBar setSearchFieldBackgroundImage:[UIImage qmui_imageWithColor:UIColorDarkGold] forState:UIControlStateNormal]; 
    
//qmui扩展UIImage的方法    
+ (UIImage *)qmui_imageWithColor:(UIColor *)color {
    return [UIImage qmui_imageWithColor:color size:CGSizeMake(4, 4) cornerRadius:0];
}

+ (UIImage *)qmui_imageWithColor:(UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius {
    size = CGSizeFlatted(size);
    CGContextInspectSize(size);
    
    color = color ? color : UIColorClear;
BOOL opaque = (cornerRadius == 0.0 && [color qmui_alpha] == 1.0);
    return [UIImage qmui_imageWithSize:size opaque:opaque scale:0 actions:^(CGContextRef contextRef) {
        CGContextSetFillColorWithColor(contextRef, color.CGColor);
        
        if (cornerRadius > 0) {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMakeWithSize(size) cornerRadius:cornerRadius];
            [path addClip];
            [path fill];
        } else {
            CGContextFillRect(contextRef, CGRectMakeWithSize(size));
        }
    }];
}


带一个分组section header的tableview,滑动到section header后不再滑动:

//禁止滑动出界
self.tableView.bounces = NO;
    
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.memberType == MEMBER_TYPE_INNER_GROUP){
        return;
    }
    //NSArray *array = [self.tableView visibleCells];
    //NSIndexPath * indexPath = [self.tableView indexPathForCell:array.firstObject];
    //NSLog(@"滑到了第 %ld 组 %ld个",indexPath.section, indexPath.row);
    long topHeaderY = self.tableView.tableHeaderView.frame.size.height;
    long scrollY = scrollView.contentOffset.y;
    if (scrollY >= topHeaderY){
        //禁止scroll
        //self.tableView.scrollEnabled = NO;
        scrollView.contentOffset = CGPointMake(0, topHeaderY);
    }
    return;
}


ViewController的预加载

    [viewController load]; 
    //[viewController loadViewIfNeeded];

呱牛笔记

本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com

请先登录后发表评论
  • 最新评论
  • 总共0条评论