iOS-Tips

作者: c6e16b2e3a16 | 来源:发表于2018-06-14 11:07 被阅读41次

    记录一些不常用,容易忘记的知识点

    1.调用系统相机显示中文

    在info.plist中添加Localized resources can be mixed这一项并设置为YES

    2.计算文字宽度(高度)
    方法一:
    CGFloat fontSize = 14;
    CGFloat height = 20;
    NSString *string = @"想要计算宽度的文字";
    CGFloat width = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size.width;
    
    方法二:
    NSString *text = @"想要计算宽度的文字";
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0f]}];
    
    3.Reveal使用

    方法一:

    1. 导入Reveal.framework
    2. Link Binary With Libraries 删除Reveal.framework
    3. Other Linker Flags + -ObjC -lz -framework Reveal
      方法二:
       pod 'Reveal-SDK', '4' //Mac版本Reveal,网上找到的是v4,这里要对应
    
    4.判断是否有权限访问相册
    #import <AssetsLibrary/AssetsLibrary.h>
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
        //无权限,提示用户打开
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"请在\"隐式设置\"中允许\"%@\"访问您的相册", @"YourAppDisplayName"] message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开系统设置
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: [NSString stringWithFormat:@"prefs:root=%@", @"YourAppBundleId"]]];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
        [self showDetailViewController:alert sender:self];
    }
    
    5.判断是否有权限访问相机
    #import <AVFoundation/AVCaptureDevice.h>
    #import <AVFoundation/AVMediaFormat.h>
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        //无权限,提示用户打开,同上
    }
    
    
    6.检测ipa包是否包含P3图片

    由于公司UI外包了,不切图,平时都是自己切图,找icon.有次在iconfont上找了个P3格式的图片,导致9.x系统的崩溃,现在每次加入新图片,上包前都要检测一下是否包含P3图片.

    参考文章:https://www.ianisme.com/ios/2409.html

    ED7ED320-496A-4C78-A1D9-1EAC605207F5.png
    1. 终端cd到 ipa包的Payload文件夹下
    2. $ find . -name 'Assets.car'
    3. $ sudo xcrun --sdk iphoneos assetutil --info ./Assets.car > /tmp/Assets.json
      (/tmp/Assets.json是导出的文件路径)
      4.找到导出的文件,查找是否含有P3格式的文件
      5.如果找到了,则需要修改文件格式,直接command+F,查找"P3"
      对,就是这张图,从iconfont


      7F355FDF-BCF0-4C63-B598-0E139B2A7F72.png

      6.修改或替换
      找UI重新切图 或
      修改图片:在项目中找到这张图,右击用"色彩同步实用工具/ColorSync"应用打开,"指派描述文件"->"sRGB IEC61966-2.1",应用


      4DDFB9F3-382E-4C5D-B22C-C25CE034AADB.png
      7.再次打包尝试
    6.UITableView

    1.修改UITableViewHeaderFooterView背景颜色

        headerView.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        headerView.backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    7.属性字符串

    https://www.jianshu.com/p/824c5163878a

    8.自定义UISegmentedControl
        UISegmentController *segmentCtrl = [[UISegmentedControl alloc] init];
        segmentCtrl.tintColor = [UIColor clearColor]; 
        //选中文字颜色
        NSDictionary *selectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:13 weight:UIFontWeightBold], NSForegroundColorAttributeName:COLOR_WITH_HEX(0X333333)}; 
        [segmentCtrl setTitleTextAttributes:selectedTextAttr forState:UIControlStateSelected];
        //未选中文字颜色
        NSDictionary *deselectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:13 weight:UIFontWeightLight], NSForegroundColorAttributeName:COLOR_WITH_HEX(0X333333)};
        [segmentCtrl setTitleTextAttributes:deselectedTextAttr forState:UIControlStateNormal];
        //调整内容位置
        CGFloat itemWidth = floor((SCREEN_WIDTH - 12*2)/4);
        CGSize size = [@"距离最近" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13.0f weight:UIFontWeightBold]}];
        CGFloat offset_x = (itemWidth - size.width)/2;
        [segmentCtrl setContentPositionAdjustment:UIOffsetMake(-offset_x, 0) forSegmentType:UISegmentedControlSegmentAny barMetrics:UIBarMetricsDefault];
        [segmentCtrl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    9.动态修改状态栏颜色(随scrollview滑动改变)
    @property (nonatomic, assign) UIStatusBarStyle statusBarStyle; //记录状态栏样式
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGFloat y = scrollView.contentOffset.y;
        if (y <= 64) {
            self.statusBarStyle = UIStatusBarStyleLightContent;
        } else {
            self.statusBarStyle = UIStatusBarStyleDefault;
        }
        [self setNeedsStatusBarAppearanceUpdate];
    }
    - (UIStatusBarStyle)preferredStatusBarStyle {
        return self.statusBarStyle;
    }
    
    10.点击事件穿透视图在下一视图响应
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
        UIView *hitView = [super hitTest:point withEvent:event];
        if (hitView == self) {
            return nil;
        }
        return hitView;
    }
    
    11.UITableView setContentOffset: 无效

    以下设置

        _tableView.estimatedRowHeight = 0;// default is UITableViewAutomaticDimension, set to 0 to disable
        _tableView.estimatedSectionHeaderHeight =0;// default is UITableViewAutomaticDimension, set to 0 to disable
        _tableView.estimatedSectionFooterHeight =0; // default is UITableViewAutomaticDimension, set to 0 to disable
    
    12.设置某几个角为圆角
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake:(0,0,200,100)];
        //右上右下圆角25
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_doneBtn.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(25, 25)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = view.bounds;
        maskLayer.path = maskPath.CGPath;
        view.layer.mask = maskLayer;
    
    13.UINavigationController push新的视图,原始图上的UIScrollView 自动回到原点

    在添加完子视图后,先执行 [self layoutIfNeeded];

        [self layoutIfNeeded];
        self.contentSize = CGSizeMake(width, height);
    

    相关文章

      网友评论

          本文标题:iOS-Tips

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