美文网首页
ios问题集中

ios问题集中

作者: 大写的空气 | 来源:发表于2017-11-20 12:07 被阅读0次

    http://modelend.com


    动态国际化

    stringWithFormat:localizedString(@"%@好")



    获取保存在沙盒中所有key

    NSUserDefaults * defs = [NSUserDefaults

    standardUserDefaults];

    NSDictionary * dict = [defs dictionaryRepresentation];

    for (id key in dict) {

    NSLog(@"userdefault:%@",key);

    }

    [defs synchronize];

    同时也可以用于删除所有key


    删除沙盒中所有key

    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];

    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

    return  [[NSUserDefaults standardUserDefaults] synchronize];


    截屏

    + (UIImage *) screenShot:(UIView*) view

    {

    UIImage *img;

    UIGraphicsBeginImageContext(view.bounds.size);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

    }


    使用URLWithString时,返回nil问题

    URL地址中包含特殊字符可能会出现此问题

    NSString *strurl = [NSString stringWithFormat:@"%@%@?ip=%@&ver=%d",@"https://stamina.hicling.net/", @"plus/iplocation",[self getExtraIP],release];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: [strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];


    大量加载图片,内存过高优化

    使用内存池,及时清空image所占内存

    for (int i = 0; i <= 1000; i ++) {

    //创建一个自动释放池

    @autoreleasepool{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"PNG"];

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];

    UIImage *image2 = [image imageByScalingAndCroppingForSize:CGSizeMake(480, 320)];

    }

    }

    如果是本地图片,尽量不要使用 [UIImage  imageNamed:nil]; 这种方式,使用这种方式加载,只要程序不退出,它便一直会在内存中。

    我们可以使用 :

    NSString*path = [[NSBundlemainBundle]pathForResource:@"图片的名字"ofType:@""];

    UIImage*image = [UIImageimageWithContentsOfFile:path];

    那两者的优缺点就很明显了,[UIImage  imageNamed:nil]; 只需加载一次,它便在内存中,所以第二次加载速度很快。而第二种加载方式由于我们将它释放掉了,会再次加载。所以选用那种方式,依你情况而定。


    使用NSTimer实现计时却不准确

    当使用NSTimer的scheduledTimerWithTimeInterval方法时。事实上此时Timer会被加入到当前线程的Run Loop中,且模式是默认的NSDefaultRunLoopMode。而如果当前线程就是主线程,也就是UI线程时,某些UI事件,比如UIScrollView的拖动操作,会将Run Loop切换成NSEventTrackingRunLoopMode模式,在这个过程中,默认的NSDefaultRunLoopMode模式中注册的事件是不会被执行的。也就是说,此时使用scheduledTimerWithTimeInterval添加到Run Loop中的Timer就不会执行。


    为了设置一个不被UI干扰的Timer,我们需要手动创建一个Timer,然后使用NSRunLoop的addTimer:forMode:方法来把Timer按照指定模式加入到Run Loop中。这里使用的模式是:NSRunLoopCommonModes,这个模式等效于NSDefaultRunLoopMode和NSEventTrackingRunLoopMode的结合


    self.animationTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(animationTimerDidFired:) userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoop] addTimer:self.animationTimer forMode:NSRunLoopCommonModes];


    循环引用

    把block创建某个类的成员属性时,self会对block产生一个强引用,block创建用的是copy,是因为block会对block内部的变量的值进行copy。这时候block会对block中的变量产生一个强引用。 如果block中有 self.xxxx 或者有属于self的成员但是没有self. 这样的话 就造成了block的循环引用问题。

    解决办法: 通过 __weak  __block 去声明一个弱引用的self 。

    _weak typeof(self) weakSelf = self;

    self.blkA = ^{

    __strong typeof(weakSelf) strongSelf = weakSelf;//加一下强引用,避免weakSelf被释放掉

    NSLog(@"%@", strongSelf->_xxView);//不会导致循环引用.

    };


    注释命名规范

    /**

    @method  函数(方法)的相关注释。

    @abstract 这里可以写一些关于这个方法的一些简要描述

    @discussion 这里可以具体写写这个方法如何使用,注意点之类的。

    如果你是设计一个抽象类或者一个共通类给给其他类继承的话,建议在这里具体描述一下怎样使用这个方法。

    @param text 文字(这里把这个方法需要的参数列出来)

    @param error 错误参照

    @result 返回结果

    */

    touch未按预期响应

    在上图class中分别加入- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event测试。MainHealthReportView为mainViewVC中的子view,其他为对应的view的category与viewcontroller的category。

    当在MainHealthReportView中做点击操作

    只会在UIView的category中响应touch事件。

    可能我们希望的响应点在MainHealthReportView或者mainViewVC。

    在MainHealthReportView中响应的方法

    只需加上super方法,能达到MainHealthReportView中响应的要求


    实现上面样式遇到问题集中

    由于需要此界面导航栏消失,简单快捷方法是[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

    将导航栏透明处理,需要配合self.navigationController.navigationBar.translucent = YES;使用,界面切换间效果不理想,由于实际需求,此代码可能放于viewWillAppear中执行。那么问题产生,右滑退出是,如果手指右滑做页面退出操作(非真正退出),重新回到原页面,发现原页面位移错乱,整体向上偏移.

    考虑隐藏导航栏,自定义一个导航栏加入视图.相关代码

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, SCREEN_W,  +44)];

    //    bar.backgroundColor = [UIColor purpleColor];

        bar.layer.shadowColor = [UIColor clearColor].CGColor;

        [barsetBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

        [barsetShadowImage:[UIImage new]];

        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:title];

        UIBarButtonItem *leftItem = [self createNavItem:[self createBackButton:@"back"] sel:nil];

        UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

        // 设置边框距离,个人习惯设为-16,可以根据需要调节

        fixedItem.width= -16;

        navItem.leftBarButtonItems=@[fixedItem, leftItem];

        //  将status bar 文本颜色设置为白色

        bar.barStyle = UIBarStyleBlack;

        // 字体大小19,颜色为白色

        [barsetTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];

        // 字体大小12,颜色为白色

        [navItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

        // 字体大小12,颜色为白色

        [navItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

        bar.items=@[navItem];

    OK,创建完毕后,加入视图,又发现statusbar位置未被覆盖

    if(@available(iOS11.0, *)) {

            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

        }else{

            // Fallback on earlier versions

        }

    此代码解决问题。同时,导航栏隐藏后,原用导航栏更改statubar文字颜色失效,本来应该是进入此页面文字为白色,回到首页为黑色,隐藏导航后此页面颜色为黑色,需要加入另外方法

    - (UIStatusBarStyle)preferredStatusBarStyle

    {

        return UIStatusBarStyleLightContent;

    }

    什么时候需要使用parentViewController?

    当一个VC通过addchild

    如果需要支持 Bitcode,需要在 Build Settings -> Deployment 中打开 DEPLOYMENT_POSTPROCESSING = YES,

    设置 STRIP_STYLE 为 Debugging Symbols,

    Build Settings -> Compiler Flags添加 -fembed-bitcode 参数。

    相关文章

      网友评论

          本文标题:ios问题集中

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