iOS开发小计

1.设置表格cell分割线的间距

self.tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);

2.获取当前控制器

/**
 获取当前控制器
 
 @return 当前控制器
 */
+ (UIViewController *)getVC
{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id  nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    //    如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC;
        while (((UIViewController *)nextResponder).presentedViewController) {
            nextResponder = ((UIViewController *)nextResponder).presentedViewController;
        }      
    }
else
{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]])
{
        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行
        result=nav.childViewControllers.lastObject;      
    }
else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }
else{
        result = nextResponder;
    }
   return result;
}

3.给UIView设置背景图片

self.view.layer.contents = (__bridge id)[UIImage imageNamed:@"login_bg"].CGImage;

4.数组转json

+ (NSString *)jsonStringFormArray:(NSArray *)array {
    NSMutableString *jsonString = [NSMutableString string];
    [jsonString appendString:@"["];
    
    for (int i=0; i

5.字典转json方法

+ (NSString *)jsonStringFormDict:(NSDictionary *)dict {
    NSMutableString *jsonString = [NSMutableString string];
    [jsonString appendString:@"{"];
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (jsonString.length > 1) {
            [jsonString appendString:@","];
        }
        [jsonString appendFormat:@"\"%@\":",key];
        
        if ([obj isKindOfClass:[NSArray class]]) {
            [jsonString appendString:[self jsonStringFormArray:obj]];
        }
        
        else {
            if ([obj isKindOfClass:[NSDictionary class]]) {
                [jsonString appendString:[self jsonStringFormDict:obj]];
            }
            else {
                [jsonString appendFormat:@"\"%@\"",obj];
            }
        }
    }];
    [jsonString appendString:@"}"];
    return jsonString;
}

6.删除字符串中的空格和换行符号

+ (NSString *)delSpaceAndNewline:(NSString *)string {
    NSMutableString *mutStr = [NSMutableString stringWithString:string];
    
    NSRange range = {0,mutStr.length};
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
}

7.判断应用的状态

UIApplicationState state = [UIApplication sharedApplication].applicationState;
UIApplicationStateActive(前台)
UIApplicationStateInactive(收到通知)    
UIApplicationStateBackground(后台)

8.TableView设置阴影

_InfoTableView.layer.cornerRadius = 10;
_InfoTableView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
_InfoTableView.layer.shadowOffset = CGSizeMake(5, 5);
_InfoTableView.layer.shadowOpacity = 0.2;
_InfoTableView.layer.shadowRadius = 5;
_InfoTableView.clipsToBounds = NO;
clipsToBounds 必须设置,否则前面的代码无效。

8.金额计算 (银行家舍入)

银行家舍入的实际例子,以下都保留两位小数:
1.2345 -> 1.23
3.456 -> 3.46
2.3452 -> 2.35 //舍入位为5,5后面有数字,直接进位
2.345 -> 2.34 //舍入位为5,5为最后一位,且前面一位为偶数(4),所以直接舍去
2.335 -> 2.34 // 舍入位为5,5为最后一位,且前面一位为奇数(3),所以进位后舍去

/**
 银行家舍入

 @param banker 数值
 @return 返回数值
 */
-(NSString *)CountWithBanker:(double)banker ;
{
    NSDecimalNumber * bankerNumber = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",banker]];
    
    if (bankerNumber.floatValue>0&&bankerNumber.floatValue<=0.01) {
        bankerNumber=[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",0.01]];
    }
    NSDecimalNumberHandler * round = [NSDecimalNumberHandler
                                      decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                      scale:2
                                      raiseOnExactness:NO
                                      raiseOnOverflow:NO
                                      raiseOnUnderflow:NO
                                      raiseOnDivideByZero:YES];
    
    NSDecimalNumber * resultDecimal = [bankerNumber decimalNumberByRoundingAccordingToBehavior:round];
    return [NSString stringWithFormat:@"%@",resultDecimal];
}

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