iOS开发笔记

作者: BeeQiang | 来源:发表于2017-11-18 11:15 被阅读0次

    人过留名,雁过留声,当自己老了回首今朝,假如有这么个记录着自己成长的简书,应该也是别有一番感受吧!记录自己成长的点点滴滴,每天进步一点点,终会达到自己梦想的彼岸!

    1、设置UITextField的placeholder字体的颜色和字号

    textField.placeholder = @"请输入用户名";  
    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
    [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    
    // <#注释#>
    

    2、创建按钮添加拖动和点击事件

    //添加点击事件
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //添加拖动事件
    [btn addTarget:self action:@selector(dragMoving:withEvent:)forControlEvents: UIControlEventTouchDragInside];
    //添加拖动结束时的事件
    [btn addTarget:self action:@selector(dragEnded:withEvent:)forControlEvents: UIControlEventTouchUpInside];
    
    /**
      *事件
      */
    //拖动过程中
    - (void)dragMoving:(UIControl *)c withEvent:ev
    {
        CGPoint point = [[[ev allTouches] anyObject] locationInView:self.view];   
        point.x = MIN(MAX(point.x, btn.width * 0.5 + 10) , self.view.width - btn.width * 0.5 - 10);//范围
        point.y = MIN(MAX(point.y, 100), self.view.height - btn.height * 0.5 - 10);//范围
        c.center = point;
        _isClick = NO;
    }
    //拖动结束
    - (void)dragEnded:(UIControl *)c withEvent:ev
    {
        XDLog(@"dragEnded....");   
        CGPoint point = [[[ev allTouches] anyObject] locationInView:self.view];
        point.x = MIN(MAX(point.x, btn.width * 0.5 + 10), self.view.width - btn.width * 0.5 - 10);//范围
        point.y = MIN(MAX(point.y, 100) , self.view.height - btn.height * 0.5 - 10);//范围
        c.center = point;
        [UIView animateWithDuration:0.2 animations:^{
            c.centerX = c.centerX < self.view.width - c.centerX ? 30 : self.view.width - 30;
        }];
        _isClick = YES;
    }
    //点击事件
    - (void)btnClick:(UIButton *)btn
    {
        if (_isClick) {
            //点击方法
        }
    }
    

    3、判断是否同一日

    - (BOOL)isSameDay:(NSDate*)date1 date2:(NSDate*)date2
    {
        NSCalendar* calendar = [NSCalendar currentCalendar];
        
        unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
        NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
        NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
            
        return [comp1 day]   == [comp2 day] &&
        [comp1 month] == [comp2 month] &&
        [comp1 year]  == [comp2 year];
    }
    

    4、禁止横屏

    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    5、电池状态栏改变颜色

     [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    
    默认的黑色(UIStatusBarStyleDefault)
    白色(UIStatusBarStyleLightContent)
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return UIStatusBarStyleLightContent;
    }
    

    6、UITableView的Group样式下顶部空白处理

    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = view;
    
    #pragma mark - 处理导航栏下1px横线
    _imageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
        if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
            return (UIImageView *)view;
        }
        for (UIView *subview in view.subviews) {
            UIImageView *imageView = [self findHairlineImageViewUnder:subview];
            if (imageView) {
                return imageView;
            }
        }
        return nil;
    }
    
    //UITableView点击一下就出现灰色但是立马消失掉。
    
    //点击那一刻可以指示出点击了哪一行,灰色停留一秒钟消失掉。
    
    //1.设置cell点击时候为灰色
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;  
    
    //2.在tableView代理方法didSelectedRow方法这样写
    
    - (void)tableView:(UITableView *)tableView didSelecteRowAtIndexPath:(NSIndexPath *)indexPath{
    
          [ tableView deselectRowAtIndexPath:indexPath animated:YES];//直接取消选中这一行
    
    }
    
    

    7、对图片尺寸进行压缩

    -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    {
        // Create a graphics image context
        UIGraphicsBeginImageContext(newSize);
        
        // Tell the old image to draw in this new context, with the desired
        // new size
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        
        // Get the new image from the context
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        // End the context
        UIGraphicsEndImageContext();
        
        // Return the new image.
        return newImage;
    }
    

    8、虚线图片

    - (UIImage *)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth
    {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
        [[UIColor clearColor] set];
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextBeginPath(context);
        CGContextSetLineWidth(context, borderWidth);
        CGContextSetStrokeColorWithColor(context, color.CGColor);
        CGFloat lengths[] = { 3, 1 };
        CGContextSetLineDash(context, 0, lengths, 1);
        CGContextMoveToPoint(context, 0.0, 0.0);
        CGContextAddLineToPoint(context, size.width, 0.0);
        CGContextAddLineToPoint(context, size.width, size.height);
        CGContextAddLineToPoint(context, 0, size.height);
        CGContextAddLineToPoint(context, 0.0, 0.0);
        CGContextStrokePath(context);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    9、禁止当前页面的返回手势

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        // 禁用返回手势
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        // 开启返回手势
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    

    10、在 button 加载网络图片

    // 1、单独加载网络图片 可以用SDWebImage 下的 "UIButton+WebCache.h"
     [btn sd_setImageWithURL:[NSURL URLWithString:model.imgurl] forState:UIControlStateNormal];
    
    // 2、加载网络图片和文字时·需要注意图片的大小
    /**
     *  异步加载图片
     */
       [[SDImageCache sharedImageCache] storeImage:btn.imageView.image forKey:urlStr toDisk:NO];
       [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urlStr] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            // 主线程刷新UI
          dispatch_async(dispatch_get_main_queue(), ^{
                CGSize imagesize;  //需要图片的大小
                UIImage *smallImage = [self imageWithImage:image scaledToSize:imagesize];//裁剪
                [btn setImage:smallImage forState:UIControlStateNormal];
                [btn setTitle:name forState:UIControlStateNormal];
                btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            });
           
        }];
    

    11、button 按钮图片和文字(图片左·文字右,文字隔图片10px)

       //当图片过大时·文字可能显示不出来·所以要把图片压缩成button一样的高度·就可以显示出来
       [btn setImage:image forState:UIControlStateNormal];
       [btn setTitle:name forState:UIControlStateNormal];
       btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
       btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
        //button 折行显示设置
        /*
         NSLineBreakByWordWrapping = 0,         // Wrap at word boundaries, default
         NSLineBreakByCharWrapping,     // Wrap at character boundaries
         NSLineBreakByClipping,     // Simply clip 裁剪从前面到后面显示多余的直接裁剪掉
         
         文字过长 button宽度不够时: 省略号显示位置...
         NSLineBreakByTruncatingHead,   // Truncate at head of line: "...wxyz" 前面显示
         NSLineBreakByTruncatingTail,   // Truncate at tail of line: "abcd..." 后面显示
         NSLineBreakByTruncatingMiddle  // Truncate middle of line:  "ab...yz" 中间显示省略号
         */
        button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        // you probably want to center it
        button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
        button.layer.borderColor = [UIColor blackColor].CGColor;
        button.layer.borderWidth = 1.0;
        
        // underline Terms and condidtions
        NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];
        
        //设置下划线...
        /*
         NSUnderlineStyleNone                                    = 0x00, 无下划线
         NSUnderlineStyleSingle                                  = 0x01, 单行下划线
         NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x02, 粗的下划线
         NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0)     = 0x09, 双下划线
         */
        [tncString addAttribute:NSUnderlineStyleAttributeName
                          value:@(NSUnderlineStyleSingle)
                          range:(NSRange){0,[tncString length]}];
        //此时如果设置字体颜色要这样
        [tncString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor]  range:NSMakeRange(0,[tncString length])];
        
        //设置下划线颜色...
        [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:(NSRange){0,[tncString length]}];
        [button setAttributedTitle:tncString forState:UIControlStateNormal];
    
    

    12、在xib(storyboard)中使用 UIScrollView, 默认是勾选了autolayout选项的,在autolayout下,iOS计算UIScrollView的contentsize的机制

    • iOS7中,需在viewDidLayoutSubviews中设置scrollView.contentSize属性
    -(void)viewDidLayoutSubviews
    {
        self.scrollView.contentSize = CGSizeMake(xx,xx);
    }
    
    • iOS8及以上,只需要在viewDidAppear方法中设置就好了
    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        self.scrollView.contentSize = CGSizeMake(xx,xx);
    }
    

    所以,如果要最低支持iOS7系统,只需在viewDidLayoutSubviews中设置contentSize属性即可。

    13、刷新框架的适配iOS11

    • 如果你使用了MJRefresh等刷新,并且你还隐藏了导航
    if (@available(iOS 11.0, *)) {
            self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
    
    //代码适配iOS11
    
    #define naviBarH ([[UIApplication sharedApplication] statusBarFrame].size.height + 44)
    #define tabBarH ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
    #define AboveIOS9  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
    // iPhone X 尺寸 375*812
    #define XY_iPhoneX (IS_IPHONE && XY_ScreenWidth == 375.f && XY_ScreenHeight == 812.f)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define SafeAreaBottomHeight (kWJScreenHeight == 812.0 ? 34 : 0)
    
    //iOS11 刷新单个cell或者刷新一组cell 的时候需要用到,不然会移动
     _tableView.estimatedRowHeight = 0;
     _tableView.estimatedSectionHeaderHeight = 0;
     _tableView.estimatedSectionFooterHeight = 0;
    
    //代码适配安全区域
    - (void)viewSafeAreaInsetsDidChange {
        [super viewSafeAreaInsetsDidChange];
         
        NSLog(@"viewSafeAreaInsetsDidChange-%@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
         
        [self updateOrientation];
    }
    - (void)updateOrientation {
        if (@available(iOS 11.0, *)) {
            CGRect frame = self.customerView.frame;
            frame.origin.x = self.view.safeAreaInsets.left;
            frame.size.width = self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right;
            frame.size.height = self.view.frame.size.height - self.view.safeAreaInsets.bottom;
            self.customerView.frame = frame;
        } else {
            // Fallback on earlier versions
        }
    }
    

    14、xcode打印的位置,方法,行数

    #ifdef DEBUG
        #if TARGET_IPHONE_SIMULATOR//模拟器
    
    #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    
        #elif TARGET_OS_IPHONE//真机
    
            #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    
            //#define NSLog(...) fprintf(stderr,"[%s-%d行] %s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:@"%@", ##__VA_ARGS__] UTF8String]);
    
        #endif
    
    #else
    
        //正式发布
        #ifdef zhengShiFaBu
    
            #define NSLog(...)
    
        #else
    
            #define NSLog(...) NSLog(__VA_ARGS__)
    
        #endif
    
    #endif
    

    15、弱引用、强引用

    #ifndef weakify
    #if DEBUG
    #if __has_feature(objc_arc)
    #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
    #else
    #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
    #endif
    #else
    #if __has_feature(objc_arc)
    #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
    #else
    #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
    #endif
    #endif
    #endif
    
    #ifndef strongify
    #if DEBUG
    #if __has_feature(objc_arc)
    #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
    #else
    #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
    #endif
    #else
    #if __has_feature(objc_arc)
    #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
    #else
    #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
    #endif
    #endif
    #endif
    

    16、UITableView

    • xib 创建cell. 先注册
    [self.tableView registerNib:[UINib
                                     nibWithNibName:NSStringFromClass([MyCell class])
                                     bundle:nil]
             forCellReuseIdentifier:ID];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
        
    
    
    • sb 创建cell. 直接
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    

    17、根据UITableView点击tableviewCell获取在当前屏幕中的坐标值

    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];   
    CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];   
    

    18、UITableView 刷新

    //一个section刷新    
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1]; //你需要更新的组数   
    [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];  //collection 相同  
    //一个cell刷新    
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];  //你需要更新的组数中的cell  
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; //collection 相同
    

    19、UITableViewcell 镶嵌 UITextField 复用的问题

    • 因为cell每次滑动过程都是从缓存池中去取·所以需要建一个数据来保存
      在cell里面用blokc把每次改变的值传过去 然后保存起来
    //1
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (block) {
            block([textField.text stringByReplacingCharactersInRange:range withString:string]);
        }
        return YES;;
    }
    
    //2
            cell. block = ^(NSString *title) {
                [self.dataDic setObject:title forKey:@(indexPath.row)];
            };
            NSArray *arr = [self.dataDic allKeys];
            if ([arr containsObject:@(indexPath.row)]) {
                cell.textField.text = [self.dataDic objectForKey:@(indexPath.row)];
            }else{
                cell.textField.text = nil;
            }
    
    

    20、手机屏幕一直亮着

    [UIApplication sharedApplication].idleTimerDisabled = YES;
    

    21、UILabel的文字里面有特殊字符的时候(数字,空格等),会自动换行的问题

    textLabel.lineBreakMode = NSLineBreakByCharWrapping;
    
    // 文字和图片混合排列
       NSTextAttachment *attach = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attach.image = [UIImage imageNamed:@"test"];
        NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"文字\uFFFC混合排列"];
        [attrString addAttribute:NSAttachmentAttributeName value:attach range:NSMakeRange(0, attrString.length)];
        
        _labeltest.attributedText = attrString;
    
    

    相关文章

      网友评论

        本文标题:iOS开发笔记

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