iOS 小功能实现总结

2018-06-12  本文已影响19人  鸢尾嵌宇

一、导航栏
1、状态栏
(1)、更改状态栏的字体颜色
参考博客:IOS上 关于状态栏的相关设置(UIStatusBar)\n
iOS状态栏使用总结
注意:tableView滑动改变颜色,可以直接看第一篇,在滑动改变的方法中,必须重新加载状态栏颜色(setNeedsStatusBarAppearanceUpdate)

(2)、通话或者开启热点,状态栏由20更改为40
解决方法(监听UIApplicationDidChangeStatusBarFrameNotification或者willChange方法):

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarFrame) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

- (void)didChangeStatusBarFrame{

//    NSLog(@"------------\n%@",[self class]);
//    NSLog(@"%ld",self.view.subviews.count);
    for (UIView *subView in self.view.subviews) {
//        NSLog(@"subview:%@-%@",subView,[subView class]);
        CGRect subFrame = subView.frame;
        CGFloat origalH = subFrame.size.height;
        CGFloat origalY = subFrame.origin.y;

        if ([subView isKindOfClass:[UIScrollView class]]) {
            if ([[UIApplication sharedApplication]statusBarFrame].size.height - 20>0) {
                subFrame.size.height = origalH - 20;
            }else{
                subFrame.size.height = origalH + 20;
            }
        }else{
            if (subFrame.size.height!=NavHeight) {
                if ([[UIApplication sharedApplication]statusBarFrame].size.height - 20>0) {
                    subFrame.origin.y = origalY - 20;
                }else{
                    subFrame.origin.y = origalY +  20;
                }
            }
        }

        subView.frame = subFrame;
    }
//    NSLog(@"~~~~~~~~");
}

当然,其实最有效的方法,还是,直接使用!!!约束!!!新项目还是使用约束。

二、scrollView
1、滑动视图从iOS 11.0 开始,因为添加了安全距离,因此初始化,最好添加下面代码:

if (@available(iOS 11.0, *)) {
        self.tableView.insetsContentViewsToSafeArea = NO;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;     

    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    if (@available(iOS 11.0, *)) {
        //如果不添加,可能头部尾部多出空白
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
    }

tableView没有头部尾部,但是仍有小部分空白,可以重置高度

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

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

三、文本框
1、UITextField
(1)、限制字数(为了限制中文)

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:_textField];

- (void)textFieldDidChange:(NSNotification *)noti{
    UITextField *textField = (UITextField *)[noti object];
    //    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
        if (!position) {
            
            NSString *str = textField.text;
            
            if (str.length>200) {
                str = [str substringToIndex:200];
            }
            
            self.textField.text = str;
            
        }// 有高亮选择的字符串,则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
    else{
        NSString *str = textField.text;
      
            if (str.length>200) {
                str = [str substringToIndex:200];
            }
        
        self.textField.text = str;
        
    }
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:_textField];
    
}

(2)、直接使用系统UITextField,输入过长字符串,会出现从半截开始,如:北京已有速电子商务科技有限公司有限公司,解决方法,也是使用上面的通知监听,在方法textFieldDidChange里,直接

- (void)textFieldDidChange:(NSNotification *)noti{
    UITextField *textField = (UITextField *)[noti object];
        NSString *str = textField.text;
        self.textField.text = str;
}

2、UITextView
(1)、限制字数
写一个公用类,进行限制,参考:https://github.com/yuanweiqianyu/ziLiao

四、扩展类目
1、UIColor

+ (UIColor *)colorWithRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue
{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}

+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];
}

2、NSString(正则验证)
https://github.com/yuanweiqianyu/ziLiao

仅做参考,如有错误,敬请指正。日后陆续更新补充。
最后更新时间:2018.06.12

上一篇下一篇

猜你喜欢

热点阅读