码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 小坑集锦

时间:2015-07-22 10:54:49      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

在做自己的第一个 iOS app,一路遇到不少困难,好在靠 Google 和 StackOverflow 都解决了,自己也不知道是否是 best practice,为了赶进度只要 work 就好。

隐藏 Tab bar

在以 Tab bar 划分模块的 app 中有些非一级界面是不需要底部的标签栏的,只需要在该 ViewController 的viewWillAppear:中加入设置标签栏隐藏的语句:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

计算 UIScrollView 的 ContentSize

有些 UIScrollView 的内容是动态增减的,这就需要重新计算 ContentSize,在改变内容后增加以下代码:

-(void)resizeScrollViewContentSize {
    [self layoutIfNeeded];
    CGRect contentRect = CGRectZero;
    for (UIView *view in self.subviews) {
        contentRect = CGRectUnion(contentRect, view.frame);
    }
    self.contentSize = CGSizeMake(contentRect.size.width, contentRect.size.height);
}

貌似必须要在计算前最好执行layoutIfNeeded,否则有些 sub view 还没有布局好。

设置 status bar 的文字颜色

iOS7以后,status bar 的背景颜色变成了透明色,而且系统会根据 app的颜色自动改变 status bar 的字体颜色(黑和白)。但是这个自动改变的字体颜色并不一定和所有的 app 都搭配,比如我们 app 的主题色是稍微浅一丢丢的蓝,但是系统匹配的 status bar 的字体颜色就是黑色,看起来就很不爽,所以就要强制将其改为白色。

  1. 首先在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance的 item,其 Type 设为 Boolean,Value 设为 NO。
  2. 然后在AppDelegate.mapplication:didFinishLaunchingWithOptions:中添加:
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

计算多行文本的高度

UILabel 和 UITextView 可以显示多行的文本,如果字符串是动态获取的话就需要计算整个文本的高度了(宽度一般是固定的),这时就要用到boundingRectWithSize: options: attributes: context:这个 API 了(iOS7新增的)。为了方便自己工程中调用,我封装了一下:

+ (CGRect)stringRect:(NSString *)string fontSize:(CGFloat)fontSize constraintWidth:(CGFloat)width constraintHeight:(CGFloat)height {
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize constraint = CGSizeMake(width, height);
    NSDictionary *attributes = @{NSFontAttributeName : font};
    return [string boundingRectWithSize:constraint
                                options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                             attributes:attributes
                                context:nil];
}

去掉字符串头尾的空格

对于 UITextField 中输入的字符串往往都要进行 trim 处理,需要用到以下代码:

NSString *result = [self..nameTextField.text
                          stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

监听 UITextView 的输入,实时显示字数

首先要 conform(遵从?实现?) UITextViewDelegate,在textViewDidChange:中实现在 UILabel 中显示当前 UITextView 中的字数:

- (void)textViewDidChange:(UITextView *)textView {
    _countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)textView.text.length];
    [self setNeedsDisplay];
}

设置 UITextView 的最大输入长度

实现UITextViewDelegate中的textView:shouldChangeTextInRange:方法:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
   // if (range.location >= kMaxTextLength) { 这样会导致移动光标后再输入最大长度就失效
    if(textView.text.length >= kMaxTextLength) {
        return NO;
    }

    return YES;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS 小坑集锦

标签:

原文地址:http://blog.csdn.net/tubiebutu/article/details/46997505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!