美文网首页
iOS开发常见问题

iOS开发常见问题

作者: 就_这样 | 来源:发表于2017-11-08 10:21 被阅读0次

    一直想总结一些实际开发中遇到的问题的解决方法,和开发细节中比较好用的方法,但是开发节奏一直很紧凑,导致今天才来完成这个心愿。为鞭策自己,以后只要已有时间或者发现好东西,会及时的更新这篇文章。也希望大家对本文提出更好的意见和你们自己的看法。

    1、iOS中的round、ceil、floor函数

    round 如果参数是小数,则求本身的四舍五入.
    ceil 如果参数是小数,则求最小的整数但不小于本身.
    floor 如果参数是小数,则求最大的整数但不大于本身.
    

    Example:如何值是3.4的话,则
    -- round 3.000000
    -- ceil 4.000000
    -- floor 3.00000

    2、iOS中代理属性为什么要用Weak修饰?

    对于weak: 指明该对象并不负责保持delegate这个对象,delegate这个对象的销毁由外部控制。
    对于strong:该对象强引用delegate,外界不能销毁delegate对象,会导致循环引用(Retain Cycles)
    对于assing:也有weak的功效。但是网上有assign是指针赋值,不对引用计数操作,使用之后如果没有置为nil,可能就会产生野指针;而weak一旦不进行使用后,永远不会使用了,就不会产生野指针。
    

    3 、iOS 11隐藏返回键文字标题方法

    // 设置返回按键文字隐藏效果
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];

    4、使用UITabBarController管理多个控制器时,想在push出的二级控制器时隐藏底部tabbar

    创建一个继承成于UINavigationController的NavigationController,并重写- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
      if (self.viewControllers.count > 0) {
          viewController.hidesBottomBarWhenPushed = YES;
      }
      return [super pushViewController:viewController animated:animated];
    }
    

    5、设置UITextField文字左边距

    方案一:
    UITextField *investAmountTF = [[UITextField alloc]init];
    investAmountTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
    //设置显示模式为永远显示(默认不显示)
    investAmountTF.leftViewMode = UITextFieldViewModeAlways;
    方案二:
    [investAmountTF setValue:[NSNumber numberWithInt:8] forKey:@"paddingLeft"];
    

    6、设置文本内容为超链接样式

    UILabel的text不能设置超链接,使用UITextView,设置UITextView的副文本属性从而达到text超链接功能

    NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:@"百度"];
    // 链接副文本属性NSLinkAttributeName value值为NSURL(最优)或NSString
    NSDictionary *attributes = @{NSLinkAttributeName:[NSURL URLWithString:@"www.baidu.com"]};
    [contentStr addAttributes:attributes range:NSMakeRange(0, contentStr.length)];
    UITextView *infoTextView = [[UITextView alloc]init];
    // 设置副文本属性
    infoTextView.attributedText = contentStr;
    // 设置超链文本颜色:linkTextAttributes  
    infoTextView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};
    //禁用编辑属性 
    infoTextView.editable = NO;
    // 设置UITextViewDelegate
    infoTextView.delegate = self;
    [alert addSubview:infoTextView];
    [infoTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(alert).offset(5);
        make.right.equalTo(alert).offset(-5);
        make.top.equalTo(alert).offset(10);
        make.bottom.equalTo(line1.mas_top);
    }];
    // 实现代理方法
    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
        return YES; // let the system open this URL
    }
    

    相关文章

      网友评论

          本文标题:iOS开发常见问题

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