iOS 开发过程遇到的问题汇总

<一> 使用TARGET_IPHONE_SIMULATOR和TARGET_OS_IPHONE宏区分模拟器和设备;
<二>自定义cell的宽度适配问题

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        NSLog(@"cell.frame = = %@",NSStringFromCGRect(self.frame));

        [self createUI];
    }
    return self;
}

- (void)layoutSubviews{

    [super layoutSubviews];

    NSLog(@"layoutSubviews  cell.frame =  %@",NSStringFromCGRect(self.frame));

    [self.bigImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 35, 0));  //设置内置边框距离
    }];

    [self.smallImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(10);
        make.top.equalTo(self.bigImageView.mas_bottom).with.offset(5);   //设置控件之间的间距
        make.size.mas_equalTo(CGSizeMake(25, 25));                              //设置size属性
    }];

    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.smallImageView.mas_right).with.offset(10);
        make.top.equalTo(self.bigImageView.mas_bottom).with.offset(0);
        make.height.equalTo(@(35));
        make.width.equalTo(@(self.frame.size.width - 45 - 50));
    }];

    [self.lastImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.top.equalTo(self.bigImageView.mas_bottom).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(30, 25));
    }];
}

打印结果:
2016-03-28 17:50:09.089 MyDemo[12475:5203659] cell.frame = = {{0, 0}, {320, 44}}
2016-03-28 17:50:09.097 MyDemo[12475:5203659] layoutSubviews cell.frame = {{0, 0}, {414, 200}}
2016-03-28 17:50:09.216 MyDemo[12475:5203659] layoutSubviews cell.frame = {{0, 0}, {414, 200}}

在初始化控件的时候,cell的frame还是默认的frame(0,0,320,44)并没有发生变化,这时我们在没有使用任何自动布局的方法(包括masonry;layoutSubviews)直接用cell的frame来布局内部的子控件就会出现屏幕不适配的问题;这种情况下我们应该使用的屏幕的宽高作为cell内部子控件布局的参考对象。

在执行layoutSubviews 方法的时候,cell的frame已经由原来的默认尺寸变为与tableView中设置的相关尺寸了,在layoutSubViews方法中我们可以使用cell的frame进行内部子控件的布局。

**<三>Xcode提示如下:
“App TransportSecurity has blocked a cleartext HTTP (http://) resource load since it isinsecure. Temporary exceptions can be configured via your app’s Info.plistfile.”**
简而言之:ATS禁止了HTTP的明文传输,因为它不安全。可以修改Info.plist文件,让它临时允许明文传输。
解决方法: 在Info.plist文件中添加”App Transport SecuritySettings”, Type为”Dictionary”;
然后在此目录下再添加”Allow Arbitray Loads”, Type 为”Boolean”,“Value”为“YES”即可。

<四>当普通视图控制器viewController 被嵌入容器控制器当中,这里的容器控制器通常指UINavigationController,导航控制器会对viewController中view 的内部控件布局产生影响:
首先介绍viewController几个属性:
1> edgesForExtendedLayout 苹果的官方文档说明:
This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
其枚举值如下:
UIRectEdgeNone = 0, // view 不会占满整个屏幕 在导航栏下面开始显示
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight //view占满延伸整个屏幕

2> automaticallyAdjustsScrollViewInsets 当viewController中加入滚动视图时,设置为NO;滚动视图会在导航栏下面开始显示

< 五 > 程序能正常运行 但是 控制台 输出警告:This application is modifying the autolayout engine from a background thread
解决方案: 把更新UI的代码 放在主线程 就可以了
这里写图片描述

<六> 多个请求返回数据 同步处理问题
没有使用 第三方网络请求框架而是使用苹果的原生api进行处理

- (void)loadAllData{
    //1. 创建网络请求 会话session
    NSURLSession *session = [NSURLSession sharedSession];
    //2. 创建一个队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //3. 创建一个队列组
    dispatch_group_t group = dispatch_group_create();
    //4. 创建多个请求任务 enter队列组当中
    for (int i = 0; i < infoArr.count; i++) {  //使用苹果原生api发送网络请求
        dispatch_group_enter(group); // 很重要 
        NSString *idStr = infoArr[i][@"id"];
        NSString *urlStr = [NSString stringWithFormat:@"%@Supplier/qryCDPFCateclass?pid=%@&visitChannel=2",newWIFIURL,idStr];
        NSLog(@"urlStr = %@",urlStr);

        NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlStr]  completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 将NSData 转JSON
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

            if (i == 0) {
                  [first addObjectsFromArray:dictionary[@"data"]];
            }else if (i == 1){
                  [second addObjectsFromArray:dictionary[@"data"]];
            }else if (i == 2){
                  [third addObjectsFromArray:dictionary[@"data"]];
            }else if (i == 3){
                  [fourth addObjectsFromArray:dictionary[@"data"]];
            }else if (i == 4){
                  [five addObjectsFromArray:dictionary[@"data"]];
            }else{
                  [six addObjectsFromArray:dictionary[@"data"]];
            }
            dispatch_group_leave(group); // 很重要
        }];
        //5. 依次启动任务
        [task resume];
    }

    // 6.将服务端返回的数据在此进行统一处理
    dispatch_group_notify(group, queue, ^{

        if (self.catId == 1) {
            ziXunArray = @[first[0],second[0],third[0],fourth[0]];
            keJianArray = @[first[1],second[1],third[1],fourth[1],five[0],[infoArr lastObject]];

        }
        // 回到主线程刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            // 6.刷新UI显示出来
            if (self.catId == 3) {
                [self createUIForKangFu];
            }

            if (self.catId == 5) {
                [self createUIFuZhu];
            }

            if (self.catId == 4) {
                [self createJiaoYuUI];
            }

            if (self.catId == 1) {
                [self createZiXunUI];
            }
        });
    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值