博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS app开发中常用技巧
阅读量:5866 次
发布时间:2019-06-19

本文共 6551 字,大约阅读时间需要 21 分钟。

hot3.png

1.退出App

- (void)exitApplication {    AppDelegate *app = [UIApplication sharedApplication].delegate;    UIWindow *window = app.window;        [UIView animateWithDuration:1.0f animations:^{        window.alpha = 0;        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);    } completion:^(BOOL finished) {        exit(0);    }];    }

必要时,我们还需要设置Plist属性

在plist设置屬性:Application does not run in background = YES

2.直接回到主界面

[self.navigationController popToRootViewControllerAnimated:YES];

当然,你也可以使用 [UIApplication shareApplication].keyWindow.rootViewController.viewControllers进行遍历回退

3.禁止UIScrollView及其子类边界弹簧效果

mWebView..scrollView.bounces = NO

4.修改状态栏颜色

第一种方法:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;self.navigationController.navigationBar.tintColor = [UIColor blackColor];if (is_ios_7_Later) {   self.view.window.frame = CGRectMake(0, 20, self.view.window.frame.size.width, self.view.window.frame.size.height - 20);  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];}

第二种方法(这方法比较简单):

UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];statusBarView.backgroundColor=[UIColor blackColor];[self.view addSubview:statusBarView];[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor colorWithHexString:@"#D3331f"];    if([[UIColor getWhiteBlackByHexString:@"#D3331f"] isEqual:[UIColor blackColor]])    {        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];    }    else    {        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];    }

5.解决UIImage的imageWithContentsOfFile获取图片非自动适配的问题

+(NSString *) pathForResource:(NSString *) picName type:(NSString *) type{        if([NSStringisEmptyOrNil:picName])    {        returnnil;    }    if(![NSStringisEmptyOrNil:type] && [NSStringtrim:type].length>0)    {                       return [selfgetBundlePath:picName type:type];            }    else    {        return [self pathForResource:picName];    }}+(NSString *) getBundlePath:(NSString *) picName type:(NSString *) type{        NSString* suggestScaleSuffix  = [UIImagegetImageScaleSuffix];        NSString * fitForPicName = [picName stringByAppendingString:suggestScaleSuffix];    NSString * path = [[NSBundlemainBundle] pathForResource:fitForPicName ofType:type inDirectory:@"myios.bundle"];        if([NSStringisEmptyOrNil:path])    {                if([@"@3x"isEqualToString:suggestScaleSuffix])        {            NSString * fitForPicName = [picName stringByAppendingString:@"@2x"];            path = [[NSBundlemainBundle] pathForResource:fitForPicName ofType:type inDirectory:@"myios.bundle"];                    }                if([NSStringisEmptyOrNil:path] && [@"@2x"isEqualToString:suggestScaleSuffix])        {            path = [[NSBundlemainBundle] pathForResource:picName ofType:type inDirectory:@"myios.bundle"];                    }            }    return path;}+(NSString *) pathForResource:(NSString *) imageFullName{        if(imageFullName==nil)    {        return  nil;    }         NSString *pathExtension = [imageFullName pathExtension];     NSString *subExts = @".png|.PNG|.9.png|.9.PNG|.jpeg|.JPEG|.jpg|.JEG|.bmp|.BMP|.gif|.GIF|.ico|.ICO|.tif|.TIF|.cur|.CUR|.xbm|.XBM";        if([NSStringisEmptyOrNil:pathExtension])    {        NSString * path = nil;        NSArray * imageSuffixes = [subExts componentsSeparatedByString:@"|"];                for (NSString * imageSuffix in imageSuffixes)        {            path = [selfgetBundlePath:imageFullName type:imageSuffix];                        if(![NSStringisEmptyOrNil:path])            {                return path;            }        }        }    elseif([subExts containsString:[@"."stringByAppendingString:pathExtension]])    {                imageFullName = [imageFullName substringWithRange:NSMakeRange(0, imageFullName.length-pathExtension.length-1)];        NSString * path = [selfgetBundlePath:imageFullName type:pathExtension];            return path;    }        returnnil;}+(NSString *)getImageScaleSuffix{    if ( [[[UIDevicecurrentDevice] systemVersion] intValue] >= 4 && [[UIScreenmainScreen] scale] == 2.0 )    {                return@"@2x";    }elseif ( [[[UIDevicecurrentDevice] systemVersion] intValue] >= 4 && [[UIScreenmainScreen] scale] == 3.0 )    {                return@"@3x";        }else{        return@"";    }}

6.解决SQlite数据库批量操作问题

批量插入:

INSERT INTO MYTABLE VALUES(.....),(.....),(.....).....(.....);

批量更新插入

REPLACE INTO MYTABLE VALUES(.....),(.....),(.....).....(.....);

查询受影响的函数

在sqlite中可以直接调用

sqlite_changes(sqlite * db);

当然你也可以使用如下语句

SELECT CHANGES();

7.iOS UITableView默认行高问题

UITableView默认secctionheader与sectionFooter高度是10,当然代理返回值是0的话依然是10,为了不影响使用0高度,将代理的返回值0.0001

关于UITableView缓存identifier 

​​​​​​​不要写成局部静态变量,建议写成全局变量,并且在初始化时使其变成和以往不重复的值

8.时间戳计算

NSDateFormatter * datefmt = [[NSDateFormatteralloc] init];        [datefmt setTimeStyle:NSDateFormatterNoStyle];        [datefmt setDateStyle:NSDateFormatterNoStyle];        NSTimeZone * timezone = [NSTimeZonetimeZoneWithName:@"Asia/Chongqing"];        [datefmt setTimeZone: timezone];                [datefmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];                NSDate * sDate = [datefmt dateFromString:@"2014-12-25 12:24:35"];                NSDate * tDate = [NSDatedate];                NSTimeInterval startTime = [sDate timeIntervalSince1970];        NSTimeInterval toTime =[tDate timeIntervalSince1970] ;        NSTimeInterval interval = round(toTime-startTime);                NSLog(@"interval=%f",interval);        NSLog(@"interval=%@",[datefmt stringFromDate:[NSDatedateWithTimeIntervalSince1970:interval ]]);

9.ARC下获取对象的引用计数

NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)myObject));

10.拦截Ajax请求,实现native与web同步与异步通信

重点是实现NSURLProtocol和跨域通信问题

11.截屏

- (UIImage *)screenshot:(UIViewController *) controller{    CGRect rect = controller.view.bounds;        UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);    CGContextRef context = UIGraphicsGetCurrentContext();    if (context == NULL) return nil;    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);        [self layoutIfNeeded];    [[controller.view layer] renderInContext:context];        UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return screenshotImage;}

 

转载于:https://my.oschina.net/ososchina/blog/679162

你可能感兴趣的文章
ListVIew中包含水平滑动控件,左右滑动时容易触发上下滑动
查看>>
Android网络连接判断与处理(转载)
查看>>
Python学习笔记(八)
查看>>
js---open打开新窗口
查看>>
maven 依赖文件 pom.xml 编译 mvn compile 运行 不用mvn exec:java -Dexec.mainClass="hello.HelloWorld"...
查看>>
Qt5 Cmake
查看>>
搭建java环境(Eclipse为例)
查看>>
Mysql远程连接配置
查看>>
170. Two Sum III - Data structure design - Easy
查看>>
git分支branch合并到主分支master
查看>>
* average vector from multiple files--Matlab
查看>>
Python3下安装Scrapy
查看>>
selenium登录界面,创建表单并填写提交
查看>>
优秀程序员要做到的十点
查看>>
清除子元素浮动方法总结
查看>>
Struts2入门3 深入学习
查看>>
js中报Uncaught TypeError: Cannot read property 'length' of undefined
查看>>
Problem G
查看>>
nginx学习之反向代理篇(六)
查看>>
sublime中Emment插件用法
查看>>