iOS零碎知识

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ai迷惑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值