美文网首页
iOS 开发小知识点

iOS 开发小知识点

作者: 梁苏珍 | 来源:发表于2017-08-22 10:59 被阅读0次

    一、调用代码使APP进入后台,达到点击Home键的效果。(私有API)

    [[UIApplication sharedApplication] performSelector:@selector(suspend)];

    suspend的英文意思:悬、挂、暂停

    //tableview 如果没有占满则清除下面的

    _tableviewKnowChrist.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

    二、获取UIWebView的高度 个人

    (void)webViewDidFinishLoad:(UIWebView *)webView  {      CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];      CGRect frame = webView.frame;      webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);  }

    三、UITableView收起键盘何必这么麻烦 一个属性搞定,效果好(UIScrollView同样可以使用) 以前是不是觉得[self.view endEditing:YES];很屌,这个下面的更屌。

    yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    四、设置UILable  的行间距  和 计算带行间距的高度

    /*

    UILabel* 展示的控件

    (NSString*)str  内容

    withFont:(float)font 字体大小

    WithSpace:(float)space 行间距

    */

    //给UILabel设置行间距和字间距

    -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(float)font WithSpace:(float)space{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = space; //设置行间距

    paraStyle.hyphenationFactor = 1.0;

    paraStyle.firstLineHeadIndent = 0.0;

    paraStyle.paragraphSpacingBefore = 0.0;

    paraStyle.headIndent = 0;

    paraStyle.tailIndent = 0;

    //设置字间距 NSKernAttributeName:@1.5f

    UIFont *tfont = [UIFont systemFontOfSize:font];

    NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

    };

    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];

    label.attributedText = attributeStr;

    }

    /*

    计算UILabel的高度(带有行间距的情况)

    (NSString*)str  内容

    withFont:(float)font 字体大小

    WithSpace:(float)space 行间距

    (CGFloat)width  UILable的宽度

    */

    //计算UILabel的高度(带有行间距的情况)

    -(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(float)font withWidth:(CGFloat)width WithSpace:(float)space{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paraStyle.alignment = NSTextAlignmentLeft;

    paraStyle.lineSpacing = space;

    paraStyle.hyphenationFactor = 1.0;

    paraStyle.firstLineHeadIndent = 0.0;

    paraStyle.paragraphSpacingBefore = 0.0;

    paraStyle.headIndent = 0;

    paraStyle.tailIndent = 0;

    UIFont *tfont = [UIFont systemFontOfSize:font];

    NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

    };

    CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

    return size.height;

    }

    五、 禁止程序运行时自动锁屏

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

    六、CocoaPods pod install/pod update更新慢的问题

    pod install –verbose –no-repo-update pod update –verbose –no-repo-update 如果不加后面的参数,默认会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。

    七、修改textFieldplaceholder字体颜色和大小

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

    八、禁止textField和textView的复制粘贴菜单

    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{    if ([UIMenuController sharedMenuController]) {      [UIMenuController sharedMenuController].menuVisible = NO;    }    return NO;}

    九、级三级页面隐藏系统tabbar 1、单个处理

    YourViewController *yourVC = [YourViewController new];yourVC.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:yourVC animated:YES];

    十、取消系统的返回手势

    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

    十一、百度坐标跟火星坐标相互转换

    //百度转火星坐标

    + (CLLocationCoordinate2D )bdToGGEncrypt:(CLLocationCoordinate2D)coord

    {

    double x = coord.longitude - 0.0065, y = coord.latitude - 0.006;

    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);

    double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);

    CLLocationCoordinate2D transformLocation ;

    transformLocation.longitude = z * cos(theta);

    transformLocation.latitude = z * sin(theta);

    return transformLocation;

    }

    //火星坐标转百度坐标

    + (CLLocationCoordinate2D )ggToBDEncrypt:(CLLocationCoordinate2D)coord

    {

    double x = coord.longitude, y = coord.latitude;

    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);

    double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);

    CLLocationCoordinate2D transformLocation ;

    transformLocation.longitude = z * cos(theta) + 0.0065;

    transformLocation.latitude = z * sin(theta) + 0.006;

    return transformLocation;

    }

    十二、添加pch文件的步聚

    1:创建新文件 ios->other->PCH file,创建一个pch文件:“工程名-Prefix.pch”:

    2:将building setting中的precompile header选项的路径添加“$(SRCROOT)/项目名称/pch文件名”(例如:$(SRCROOT)/LotteryFive/LotteryFive-Prefix.pch)

    3:将Precompile Prefix Header为YES,预编译后的pch文件会被缓存起来,可以提高编译速度

    十三、关于Masonry

    a:

    make.equalTo 或 make.greaterThanOrEqualTo (至多) 或 make.lessThanOrEqualTo(至少)

    make.left.greaterThanOrEqualTo(label);

    make.left.greaterThanOrEqualTo(label.mas_left);

    //width >= 200 && width <= 400

    make.width.greaterThanOrEqualTo(@200);

    make.width.lessThanOrEqualTo(@400)

    b:masequalTo 和 equalTo 区别:masequalTo 比equalTo多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。特别是多个属性时,必须使用equalTo

    c:一些简便赋值

    // make top = superview.top + 5, left = superview.left + 10,

    // bottom = superview.bottom - 15, right = superview.right - 20

    make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))

    // make width and height greater than or equal to titleLabel

    make.size.greaterThanOrEqualTo(titleLabel)

    // make width = superview.width + 100, height = superview.height - 50

    make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))

    // make centerX = superview.centerX - 5, centerY = superview.centerY + 10

    make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

    d:and关键字运用

    make.left.right.and.bottom.equalTo(superview);

    make.top.equalTo(otherView);

    e:优先;优先权(.priority,.priorityHigh,.priorityMedium,.priorityLow)

    .priority允许您指定一个确切的优先级

    .priorityHigh 等价于UILayoutPriorityDefaultHigh

    .priorityMedium 介于高跟低之间

    .priorityLow 等价于UILayoutPriorityDefaultLow

    实例:

    make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();

    make.top.equalTo(label.mas_top).with.priority(600);

    g:使用mas_makeConstraints创建constraint后,你可以使用局部变量或属性来保存以便下次引用它;如果创建多个constraints,你可以采用数组来保存它们

    // 局部或者全局

    @property (nonatomic, strong) MASConstraint *topConstraint;

    // 创建约束并赋值

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {

    self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);

    make.left.equalTo(superview.mas_left).with.offset(padding.left);

    }];

    // 过后可以直接访问self.topConstraint

    [self.topConstraint uninstall];

    h:mas_updateConstraints更新约束,有时你需要更新constraint(例如,动画和调试)而不是创建固定constraint,可以使用mas_updateConstraints方法

    - (void)updateConstraints {

    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {

    make.center.equalTo(self);

    make.width.equalTo(@(self.buttonSize.width)).priorityLow();

    make.height.equalTo(@(self.buttonSize.height)).priorityLow();

    make.width.lessThanOrEqualTo(self);

    make.height.lessThanOrEqualTo(self);

    }];

    //调用父updateConstraints

    [super updateConstraints];

    }

    i:mas_remakeConstraints更新约束,mas_remakeConstraints与mas_updateConstraints比较相似,都是更新constraint。不过,mas_remakeConstraints是删除之前constraint,然后再添加新的constraint(适用于移动动画);而mas_updateConstraints只是更新constraint的值。

    - (void)changeButtonPosition {

    [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {

    make.size.equalTo(self.buttonSize);

    if (topLeft) {

    make.top.and.left.offset(10);

    } else {

    make.bottom.and.right.offset(-10);

    }

    }];

    }

    十四、UIWebView在IOS9下底部出现黑边解决方式

    UIWebView底部的黑条很难看(在IOS8下不会,在IOS9会出现),特别是在底部还有透明控件的时候,隐藏的做法其实很简单,只需要将opaque设为NO,背景色设为clearColor即可

    十五、数组逆序遍历

    1、枚举法

    NSArray *array = @[@"1",@"2",@"3",@"5",@"6"];

    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

    NSLog(@"%@",obj);

    }];

    2、for循环

    NSArray*array=@[@"1",@"2",@"3",@"5",@"6"];

    for (NSInteger index = array.count-1; index>=0; index--) {

    NSLog(@"%@",array[index]);

    }

    十六、把时间字符串2015-04-10格式化日期转为NSDate类型

    参考答案:

    NSString *timeStr = @"2015-04-10";

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy-MM-dd";

    formatter.timeZone = [NSTimeZone defaultTimeZone];

    NSDate *date = [formatter dateFromString:timeStr];

    // 2015-04-09 16:00:00 +0000

    NSLog(@"%@", date);

    十七 、简单写出增、删、改、查的SQL语句。

    参考答案:

    数据库的简单操作,还是会的,大学可没白学。

    增:

    insert into tb_blogs(name, url) values('aaa','http://www.baidu.com');

    删:

    delete from tb_blogs where blogid = 1;

    改:

    update tb_blogs set url = 'www.baidu.com' where blogid = 1;

    查:

    select name, url from tb_blogs where blogid = 1;

    相关文章

      网友评论

          本文标题:iOS 开发小知识点

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