iOS零碎知识

来源:互联网 发布:js改变div的style 编辑:程序博客网 时间:2024/04/28 11:49

1:子线程不能参与主线程的操作。。

犯过的错误----本想用此方法异步加载一张图片到主页。

<span style="font-size:18px;">[NSURLConnection sendAsynchronousRequest:request                                      queue:queue                          completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {                               UIImageView *imageview = [[UIImageView alloc]initWithFrame:self.view.bounds];                               [self.view addSubview:imageview];                              UIImage *image = [UIImage imageWithData:data];                               imageview.image = image;}];</span>


但是他不起作用。就是因为此操作都是在子线程内

更改后的代码:

<span style="font-size:18px;">[NSURLConnection sendAsynchronousRequest:request                                     queue:queue                         completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {                             dispatch_async(dispatch_get_main_queue(), ^{                                 UIImageView *imageview = [[UIImageView alloc]initWithFrame:self.view.bounds];                                  [self.view addSubview:imageview];                                 UIImage *image = [UIImage imageWithData:data];                                  imageview.image = image;                              });}];</span>


2:YES 当页面加载的第一个view是scrollview的时候会给scrollview留出navBar所占用的44像素

self.automaticallyAdjustsScrollViewInsets =YES;


3:避免block内的循环引用

一般使用

 __weakViewController *blockSelf =self;或者

__weak typeof(self) blockSelf =self;

指向对象的弱引用。。。。

4:objc_msgSend 报错

我在使用MJRefresh的时候在这个方法这儿报错为此方法参数过多。

解决方法为在target选择Enable Strict Checking of objc_msgSend Calls 改为NO

id objc_msgSend(idself,SEL op, ...)

此方法为发送消息。第一个参数是接收参数,第二个是本类的方法--若是在本类找不到,他会按继承体系向上查找

MJRefresh的是这样填写参数的

objc_msgSend(self.beginRefreshingTaget,self.beginRefreshingAction,self);

5:加载倍图

UIImage imageWithContentsOfFile:<#(NSString *)#>

 UIImage imageNamed:<#(NSString *)#>
的区别就是imageWithContentsOfFile加载的是路径;
imageNamed加载图片会在程序内有缓存,

    [UIImageimageNamed:@"image"];

此方法也可以加载倍图  @2x  的图片,//需要注意的是加载倍图的话,不可以在图片名字后加.png


6:隐藏导航栏和状态栏需要两段代码
<span style="font-size:18px;"> [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];        statusHiden = YES;        [self.navigationController setNavigationBarHidden:YES animated:YES];        [self setNeedsStatusBarAppearanceUpdate];//当没有nav的时候这句必不可少</span>

<span style="font-size:18px;">- (BOOL)prefersStatusBarHidden {    return self.statusHiden;}</span>

7:

+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;

第一个参数format的string:

@"V:|-80-[_imageView(80)]-[_label]"

V表示垂直
H表示水平——默认是水平@"|-80-[_imageView(80)]"
|表示父视图
-表示分割imageView与父视图之间的 -表示20像素,image与label之间的-表示8像素@"V:|-[_imageView(80)]-[_label]"

_imageView(80)表示高度或者宽度是80

>=,<=,==表示值大于等于,小于等于或者等于@"|-(==80)-[_imageView(<=80)]"

@"V:|-[_imageView(_label)]"表示高度一样

第二个参数是格式

第三个参数为空。

第四个参数为NSDictionaryOfVariableBindings(_imageView, _label);

    _imageView.translatesAutoresizingMaskIntoConstraints =NO;这个属性必须为NO

@"V:|-80-[_imageView(80)]-[_label(>=30@100)]"

label高度大于等于30最大值为100——@表示最大值——测试得@的功能没有什么用


8:空宏一般用于做条件编译

9:iOS8的自动旋转实现

<span style="font-size:18px;">- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];        switch (orientation) {            case UIInterfaceOrientationLandscapeLeft:                NSLog(@"left");                break;            case UIInterfaceOrientationLandscapeRight:                NSLog(@"right");                break;            case UIInterfaceOrientationPortrait:                NSLog(@"portrait");                break;            default:                break;        }    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {            }];    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];}</span>


10:加入第三方库或者使用svn的时候会造成文件大的难以想象

使用

cd到此文件夹

删除此文件夹下的所有.svn 和 .git 文件 

find . -name ".git" | xargs rm -Rf

find . -name ".svn" | xargs rm -Rf



0 0
原创粉丝点击