美文网首页杂乱
iOS 问题记录

iOS 问题记录

作者: upworld | 来源:发表于2018-08-02 14:13 被阅读81次
    1,NSJSONSerialization 序列化对象包含Nan 造成NSInvalidArgumentException

    知识点

    [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:NULL] 
    如果jsonObject 里面有Nan的熟悉,序列化会奔溃。
    
    image.png
    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid number value (NaN) in JSON write'
    
    2,调用yy_color 构造的NSAttributedString,在YYLabel上显示没有问题。但是在UILabel上显示不出来。
    3,NSAttributedString 算出来的高度,YYLabel显示不完整,因为遇到表情字符需要把计算出的高度+4。
    4,YYLabel 设置行数,然后设置NSAttributedString,如果超出行数会自动 ... (即lineBreakMode =NSLineBreakByTruncatingTail)。如果是UILabel ,则需要在设置NSAttributedString,再调用lineBreakMode =NSLineBreakByTruncatingTai 方可以显示...
    5, popToRootViewControllerAnimated导致tabbar白屏的问题参考链接

    在 iOS 6 以上使用UINavigationController使用popToRootViewControllerAnimated并且需要将 tab 切换到某个 index 的时候,如果这么用

    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.tabBarController setSelectedIndex:2];
    

    会导致 tabbar 被 hidden ,结果是出现 tabbar 部分出现一片白色。
    解决方案是将行代码的顺序对调也就是

    [self.tabBarController setSelectedIndex:2];
    [self.navigationController popToRootViewControllerAnimated:YES];
    

    先设置 index ,在来 popToRoot 这样就可以避免这个问题啦

    6,Pod init 失败。RuntimeError - [Xcodeproj] Unknown object version

    问题

    image.png

    解决办法:gem update xcodeproj

    image.png
    6,format %.xf万会四舍五入。需要根据需求决定是否四舍五入
         self.scanNumLabel.text = [NSString stringWithFormat:@"%.1f万",item.likeNum/10000.0];//点赞数量
    
    7,UIScrollerView 调用下面方法的时候,如果[scrollView setContentOffset:pointA animated:YES],然后立即获取 调用 [scrollView contentOffset]获取的值pointB并不是PointA
    - (void)setContentOffset:(CGPoint)contentOffset animated:(__unused BOOL)animated
    
    8,UICollectionView 调用reloadData,并不会立即执行collectionView:cellForItemAtIndexPath:,而且此时如果尝试获取collectionView的ContentSize,并不是reloadData之后的值。如果想获取reloadData之后的ContentSize可以调用下面方法
    self.collectionView.collectionViewLayout.collectionViewContentSize.height
    
    9,UIViewController 再调用 loadView方法前,在外部给viewController.view = testView 赋值的话。系统就不会调用loadView方法了。
    10,直接调用[[SDWebImageManager sharedManager].imageDownloader downloadImageWithURL 下载的图片不会缓存到 SDImageCache里面。如果需要缓存的话需要调用 [[SDWebImageManager sharedManager] loadImageWithURL 或者 SDWebImagePrefetcher
     [[SDWebImageManager sharedManager].imageDownloader 
    downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageRetryFailed progress:nil completed:nil] 
    
        [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageRetryFailed progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
        }];
    或者
    [[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:@[ graffiti, sizeSnapshot, gif ]];
    
    11, 重写layoutSubviews的时候,一定别忘了调用 [super layoutSubviews]。否则有可能导致未定义行为。有一个UICollectionViewCell 重写了layoutSubviews忘了调用[super layoutSubviews],导致cell的contentView frame = CGRectzero。导致点击事件无法响应
    12, 合理利用布局优先级,很好支持UICollectonView 内容少的时候被内容撑起,内容比较多的时候限定最大高度。让内容可以滚动。

    make.height.equalTo(self.contentLabel.mas_height).priority(250);
    make.height.lessThanOrEqualTo(@(kScreenHeight * 2/3.0)).priority(750);

    @interface IKFMPrepareTypeTipVC ()
    
    @property (nonatomic, strong) UIView *contentView;
    @property (nonatomic, strong) UILabel *topLabel;
    @property (nonatomic, strong) UILabel *contentLabel;
    @property (nonatomic, strong) UIView *bottomLineView;
    @property (nonatomic, strong) UIButton *knowBtn;
    @property (nonatomic, strong) UICollectionView *collectionView;
    @property (nonatomic, strong) NSMutableAttributedString *mutStr;
    @end
    
    @implementation IKFMPrepareTypeTipVC
    
    - (void)dealloc{
        _collectionView.delegate = nil;
        _collectionView.dataSource = nil;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view addSubview:self.contentView];
        [self.contentView addSubview:self.topLabel];
        [self.contentView addSubview:self.collectionView];
        [self.collectionView addSubview:self.contentLabel];
        [self.contentView addSubview:self.bottomLineView];
        [self.contentView addSubview:self.knowBtn];
        [self configLayout];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [self.view addGestureRecognizer:tapGesture];
    }
    
    //self.contentView 高度内部撑起
    - (void)configLayout{
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(280 * kRate));
            make.centerX.equalTo(self.view);
            make.centerY.equalTo(self.view);
        }];
        [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@25);
            make.centerX.equalTo(self.contentView);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topLabel.mas_bottom).offset(12);
            make.left.equalTo(@20);
            make.right.equalTo(@-20);
            make.height.equalTo(self.contentLabel.mas_height).priority(250);
            make.height.lessThanOrEqualTo(@(kScreenHeight * 2/3.0)).priority(750);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.collectionView);
        }];
        [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.collectionView.mas_bottom).mas_offset(12);
            make.left.right.equalTo(self.contentView);
            make.height.equalTo(@0.5);
        }];
        [self.knowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@49);
            make.left.right.equalTo(@0);
            make.top.equalTo(self.bottomLineView.mas_bottom);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        if (self.mutStr) {
            self.contentLabel.attributedText = self.mutStr;
    //        [self refreshWhenContentChange];
        }
    }
    

    内容比较多时


    image.png

    内容比较少时


    image.png
    13, UITextView 设置 text/attributedText的时候系统会调用setSelectedRange 设置光标位置。一般需要先设置text/attributedText 然后设置selectedRange。如果想控制UITextView的内容某些高亮,需要重写。需要重写- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text。然后重写attributedText。注意range的length = 0,表示插入内容。如果length > 0。则替换字符串。NSMutableAttributedString的
    - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
    

    方法比较奇怪。如果attrString的length != range.length.会重复attrString的字符直到填充的length = range.length。如果replace的range.length != attrString.length .则先调用

          [mutAtt deleteCharactersInRange:range];
            [mutAtt insertAttributedString:insertAtt atIndex:range.location];
    
    15, [self.textLabel sizeToFit] 然后紧接着调用约束如:
    [self.textLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(self.labelWidth));
     }]; 
    

    会出现跳动问题。解决办法,用字符串计算高度,然后调用约束

    13, pod 用长了之后,会缓存好多不同版本的库,如果磁盘空间不足,可以删除~/Library/Caches/Cocoapods 文件夹

    open ~/Library/Caches/Cocoapods

    14,boundingRectWithSize 算出来的高度,在Label上是完美适配的

    UILabel渲染结果


    image.png

    UITxtView 渲染结果:


    image.png
    15,限制UITextField的输入字数(长度)最正确的方法

    (链接)[https://blog.csdn.net/hbblzjy/article/details/51969376];

    16,ARMv7低端机型奇葩奔溃

    //armv7 上下面写法奔溃

    + (void)requestTLDetailsWithFeedID:(uint64_t)feedID completion:(TLDetailsCompletion)completion;
    + (void)requestTLDetailsWithFeedID:(NSInteger)feedID completion:(TLDetailsCompletion)completion {
    
    17,Load hook方法的时候不要用前向声明
    Class HallTabViewController;
    @implementation UIViewController (TabbarHandle)
    
    + (void)load {
        swizzleMethod([HallTabViewController class], @selector(viewDidAppear:), @selector(viewDidAppear_tabbar:));
        swizzleMethod([IKFollowViewController class], 
    }
    - (void)viewDidAppear_tabbar:(BOOL)animated {
        [self viewDidAppear_tabbar:animated];
        }
    @end
    

    正确写法如下:

    #import "HallTabViewController.h"
    @implementation UIViewController (TabbarHandle)
    
    + (void)load {
        swizzleMethod([HallTabViewController class], @selector(viewDidAppear:), @selector(viewDidAppear_tabbar:));
    }
    - (void)viewDidAppear_tabbar:(BOOL)animated {
        [self viewDidAppear_tabbar:animated];
        }
    @end
    
    19,在UICollectionView 中不要出现 自定义cell 里面包含另一个自定义cell。如果非要addSubview另一个cell,则需要[self.contentView addSubview:cell.contentView]; 否则会出现渲染不出来问题。

    如下面代码:

    @interface IKRecommendDecorateChannelCell ()
    @property (nonatomic, strong) IKRecommendChannelCell *innerCell;
    @end
    
    @implementation IKRecommendDecorateChannelCell
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            CGRect contentFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(6, 6, 26, 6));
            IKRecommendChannelCell *cell = [[IKRecommendChannelCell alloc] initWithFrame:contentFrame];
            [self.contentView addSubview:cell];
            self.innerCell = cell;
        }
        return self;
    }
    
    20,不知道从什么系统开始 iPhoneX系列机型。在下面情况下push 一个控制器,底部tabbar 会延迟消失。
    image.png

    如果UIViewControllerB present 一个navigationController,然后dismiss 之后,然后push进入下一个界面vc,设置vc.hidesBottomBarWhenPushed = YES。但是进入vc的时候tabbar 会展示0.3之后才消失。

    问题原因:是navigationController dismiss 之后,在push vc的时候。系统调用了UITabBarController 的viewWillLayoutSubviews.导致UITabbar 重新layout,调整了tabbar的frame导致了push之后之后tabbar显示了出来。然后系统会调用hiden又隐藏了。下次在push就好了。猜测系统内部状态出问题导致。

    解决办法:在UIViewControllerB 的viewDidAppear:调用layout

        if(self.navigationController.viewControllers.count == 1){
            [self.tabBarController.tabBar forceShow];
            //修复iPhoneX上,UITabbarController的viewcontroller模态出带导航的控制器,然后消失。push下一个界面的时候,一瞬间显示tabbar
            [self.tabBarController.view setNeedsLayout];
            [self.tabBarController.view layoutIfNeeded];
        }
    
    20,swizzleMethod, 如有一个基类ClassBase 有一个方法swizzle_methodA,ClassA,ClassB 分别继承ClassBase。

    如果用下面方法调用会出现
    1,ClassA.swizzle_methodA => ClassBase.swizzle_methodA,
    2,ClassB.swizzle_methodA => ClassA.swizzle_methodA。

    而我们期望中的效果是
    1,ClassA.swizzle_methodA => ClassBase.swizzle_methodA
    2,ClassB.swizzle_methodA => ClassBase.swizzle_methodA

    解决办法1,是分别在ClassA,ClassB中重写swizzle_methodA。
    解决方法2,

    @implementation HallTabbarHandle
    + (void)load {
        swizzleMethod([ClassA class], @selector(methodA), @selector(swizzle_methodA));
        swizzleMethod([ClassB class], @selector(methodA), @selector(swizzle_methodA));
    }
    @end
    
    void swizzleMethod(Class className, SEL originalSelector, SEL swizzledSelector) {
      // the method might not exist in the class, but in its superclass
      Method originalMethod = class_getInstanceMethod(className, originalSelector);
      Method swizzledMethod = class_getInstanceMethod(className, swizzledSelector);
    
      // class_addMethod will fail if original method already exists
      BOOL didAddMethod = class_addMethod(className, originalSelector,
                                          method_getImplementation(swizzledMethod),
                                          method_getTypeEncoding(swizzledMethod));
    
      // the method doesn’t exist and we just added one
      if (didAddMethod) {
        class_replaceMethod(className, swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
      } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
      }
    }
    

    相关文章

      网友评论

        本文标题:iOS 问题记录

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