iOS开发:常用、实用的方法和技巧

  • 判断字符串是不是以中文开头
 - (BOOL)isChineseFirst:(NSString *)firstString {
    //是否以中文开头(unicode中文编码范围是0x4e00~0x9fa5)
    int utfCode = 0;
    void *buffer = &utfCode;
    NSRange range = NSMakeRange(0, 1);
    //判断是不是中文开头的,buffer->获取字符的字节数据 maxLength->buffer的最大长度 usedLength->实际写入的长度,不需要的话可以传递NULL encoding->字符编码常数,不同编码方式转换后的字节长是不一样的,这里我用了UTF16 Little-Endian,maxLength为2字节,如果使用Unicode,则需要4字节 options->编码转换的选项,有两个值,分别是NSStringEncodingConversionAllowLossy和NSStringEncodingConversionExternalRepresentation range->获取的字符串中的字符范围,这里设置的第一个字符 remainingRange->建议获取的范围,可以传递NULL
    BOOL isChinese = [firstString getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
    if (isChinese && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))
        return YES;
    else
        return NO;
}
  • 键盘回收
 - (void)setUpForDismissKeyboard {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *operation = [NSOperationQueue mainQueue];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {
        [self.view addGestureRecognizer:tapGesture];
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {
        [self.view removeGestureRecognizer:tapGesture];
    }];
}
  • 禁止侧滑返回上一个界面
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
  • 隐藏导航栏最下面的黑线
 - (void)hiddenNavLine {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
                UIView *view =   (UIView*)obj;
                for (id obj2 in view.subviews) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *image =  (UIImageView*)obj2;
                        image.hidden = YES;
                    }
                }
            }
        }
    }
}
  • 去除自带的searchBar的背景View
 - (void)removeSearchBarBackGroundView {
    for(int i =  0 ;i < _searchBar.subviews.count;i++){
        UIView * backView = _searchBar.subviews[i];
        if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
            [backView removeFromSuperview];
            [_searchBar setBackgroundColor:[UIColor clearColor]];
            break;
        } else {
            NSArray * arr = _searchBar.subviews[i].subviews;
            for(int j = 0;j < arr.count; j++){
                UIView * barView = arr[i];
                if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
                    [barView removeFromSuperview];
                    [_searchBar setBackgroundColor:[UIColor clearColor]];
                    break;
                }
            }
        }
    }
}
  • 标签栏的背景图显示错乱问题解决办法:
//将图片如下设置
[[UIImage imageNamed:@"[email protected]"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  • 网络请求设置超时请求(基于AFNetworking3.0封装的GET,POST请求用方法)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 15.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
  • 实用的获取图片的方法,防止内存溢出:
// 建议使用该方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"图片" ofType:@"png"];  
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];

// 不建议使用该方法:
UIImage *image = [UIImage imageNamed:@"图片.png"];

你可能感兴趣的:(iOS开发)