美文网首页
iOS的各种小bug,小细节,小技巧

iOS的各种小bug,小细节,小技巧

作者: 达摩君 | 来源:发表于2017-08-04 14:47 被阅读59次

    1.CABasicAnimation按home键回到前台,再进来动画结束了。

     basicAnima.removedOnCompletion = NO; //加这句就可以完美解决
    

    2.导航栏从无到有过渡

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    }
    

    3.全局修改控件颜色

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        self.window.tintColor = [UIColor redColor];
        return YES;
    }
    
    结果样子

    4.修改UIAlertController的标题,内容对齐

        UIAlertController *alertV = [UIAlertController alertControllerWithTitle:@"收到就好方法\n 还多久发货的尽快回复" message:@"dfhdfjjj你读几\n年级开发计划的房价的\n发挥发动机大家疯狂的空间房间打开房间看到几分就看到房间空间的空间发快捷的接口附近的房价肯定艰苦奋斗讲课费" preferredStyle:UIAlertControllerStyleAlert];
        UILabel *titleLable = alertV.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews[0];
        UILabel *messageLable = alertV.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews[1];
        titleLable.textAlignment = NSTextAlignmentLeft;
        messageLable.textAlignment = NSTextAlignmentLeft;
        [self presentViewController:alertV animated:YES completion:nil];
    

    5.Swift判断版本

          if #available(iOS 9.0, *) {
                print("iOS9")
            } else {
                print("ios8")
            }
    
      @available(iOS 9.0, *)
      方法名
    

    6.AppStore中文审核指南

    地址

    7.导航条回退按钮自定义后,回退手势失效

    class LEENavController: UINavigationController, UINavigationControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate; //一句话解决
        }
    }
    

    8.Tabbar首页双击刷新

    //在AppDelegate.m文件中设置UITabBarControllerDelegate,最好设置window为代理,不要在TabBarController中设置代理为自己。
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"doubleClickDidSelectedNotification" object:nil];
    }
    
    //在需要刷新的控制器中接听通知
    @property(nonatomic, weak) UIViewController *lastVC;
    @property(nonatomic, strong) NSDate *lastDate;
    - (void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataAction2) name:@"doubleClickDidSelectedNotification" object:nil];
    }
    - (void)reloadDataAction2 {
        
        _lastVC = self.tabBarController.selectedViewController;
        if ([self.lastVC isKindOfClass:[self.navigationController class]] && self.view.window) {
            
            NSDate *date = [[NSDate alloc]init];
            if (date.timeIntervalSince1970 - _lastDate.timeIntervalSince1970 < 0.5) {
                [self.contentModels removeAllObjects];
                [self.dataArrays removeAllObjects];
                [self reload];
            }
            _lastDate = date;
        }
    }
    

    9.Xcode模拟器实现3DTouch

    按住control键或者command键 + 鼠标点击icon.(模拟器在6s版本以上哦)

    10.iOS应用内通知样式

    UNNotificationPresentationOptionBadge  
    UNNotificationPresentationOptionSound   
    UNNotificationPresentationOptionAlert
    

    这3中应用通知样式只支持iOS10.0以上。iOS10以下的应用内通知没有alert之类的。要自己自定义弹框!!!(由于自己模模糊糊的记得好像在iOS8调试机应用内上看到过系统弹框,所以蒙着头搞了好久,浪费了一大堆时间。印象这东西太可怕了!!!)

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    //弹框吧
    }
    

    11.iOS请求链接包含中文解决方法

    NSString *urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    12.swift3.0 Range转NSRange

    发现简书里面解决方案好多都不能运行的

    第一种
            let str = "😆你好啊,ljb hhshshss"
            let nsText = str as NSString
            let textRange = NSMakeRange(0, nsText.length)
            let attrributedString = NSMutableAttributedString(string: str)
            
            nsText.enumerateSubstrings(in: textRange, options: .byWords) { (substring, substringRange, _, _) in
                
                if (substring == "ljb") {
                    attrributedString.addAttributes([NSForegroundColorAttributeName: UIColor.red], range: substringRange)
                }
            }
            sssss.attributedText = attrributedString
    
    第二种
    //String分类
    extension String {
        func NSRangeFromRange(range : Range<String.Index>) -> NSRange {
            let utf16view = self.utf16
            let from = String.UTF16View.Index(range.lowerBound, within: utf16view)
            let to = String.UTF16View.Index(range.upperBound, within: utf16view)
            return NSMakeRange(from - utf16view.startIndex, to - from)
        }
    }
    
    override func viewDidLoad() {
            super.viewDidLoad()
            
            let str = "😆你好啊,ljb hhshshss"
            let attrributedString = NSMutableAttributedString(string: str)
            let range = str.range(of: "hh")
            attrributedString.addAttributes([NSForegroundColorAttributeName: UIColor.red], range: str.NSRangeFromRange(range: range!))
            sssss.attributedText = attrributedString
        }
    
    

    13.长按TextField或TextView显示中文的粘贴复制

    在info.plist中添加Localized resources can be mixed = YES就好

    14. 怎么删除URL Types的选项

    发现在TARGETS-->Info --> URL Types下面没有删除按钮。解决方法要到info.plist中URL types下删除就好

    15.修改约束,没有动画问题

    原代码

        [UIView animateWithDuration:0.5 animations:^{
            
            self.viewTopConstraint.constant =  140 * SCREEN_SIZE_Width;
        }];
    

    没效果,只需要再增加一句[self layoutIfNeeded]

       [UIView animateWithDuration:0.5 animations:^{
            
            self.viewTopConstraint.constant =  140 * SCREEN_SIZE_Width;
            [self layoutIfNeeded];
        }];
    

    16.TabBar去掉上面的一条横线

        self.tabBar.translucent = NO;
        [[UITabBar appearance]setShadowImage:[UIImage new]];
        [[UITabBar appearance]setBackgroundImage:[UIImage new]];
    

    17.修改tabbarItem的icon图片位置和标题位置

    Vc.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0); 
    Vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
    

    18.修改TabbarItem的文字大小,颜色

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : TCHDarkGrayFontColor, NSFontAttributeName : [UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : TCH46BCFF, NSFontAttributeName : [UIFont systemFontOfSize:10]} forState:UIControlStateSelected];
    

    19.2个日期相差的天数

    - (NSInteger)dayWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        [calendar setFirstWeekday:2];
        NSDate *fDate;
        NSDate *tDate;
        //去掉时分秒
        [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fDate interval:NULL forDate:fromDate];
        [calendar rangeOfUnit:NSCalendarUnitDay startDate:&tDate interval:NULL forDate:toDate];
        
        NSInteger day = [calendar components:NSCalendarUnitDay fromDate:fDate toDate:tDate options:NSCalendarWrapComponents].day;
        return day;
    }
    

    20.获取APPStroe版本号和本地版本号

    + (void)checkUpDateWithByAppId:(NSString *)appID success:(void (^)(NSDictionary *, BOOL, NSString *, NSString *))success failure:(void (^)(NSError *))failure {
        
        NSString *encodingUrl = [[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionTask *sessionTask = [session dataTaskWithURL:[NSURL URLWithString:encodingUrl] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            if (error != nil) {
                failure(error);
                return ;
            }
            NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSString *versionStr = resultDic[@"results"][0][@"version"];
            NSString *versionStr_int = [versionStr stringByReplacingOccurrencesOfString:@"." withString:@""];
            int version = [versionStr_int intValue];
            
            //加载程序中的info.plist文件获得当前程序的版本号
            NSString *nowValueCode = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
            NSString *currentVersionStr_int = [nowValueCode stringByReplacingOccurrencesOfString:@"." withString:@""];
            int current = [currentVersionStr_int intValue];
            if (version > current) {
                success(resultDic, YES, versionStr, nowValueCode);
            } else {
                success(resultDic, NO, versionStr, nowValueCode);
            }
        }];
        [sessionTask resume];
        
    }
    

    21.

    相关文章

      网友评论

          本文标题:iOS的各种小bug,小细节,小技巧

          本文链接:https://www.haomeiwen.com/subject/ydgvlxtx.html