iOS被坑集锦

移动开发
在做自己的第一个 iOS app,一路遇到不少困难,好在靠 Google 和 StackOverflow 都解决了,自己也不知道是否是 best practice。

在做自己的***个 iOS app,一路遇到不少困难,好在靠 Google 和 StackOverflow 都解决了,自己也不知道是否是 best practice。

隐藏 Tab bar

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

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

但是,更好的作法是在 push 一个 ViewController 之前,将其属性hidesBottomBarWhenPushed设置为YES:

  1. SomeViewController *svc = [SomeViewController new]; 
  2. svc.hidesBottomBarWhenPushed = YES; 
  3. [self.navigationController pushViewController:svc animated:YES]; 

计算 UIScrollView 的 ContentSize

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

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

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

计算多行文本的高度

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

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

去掉字符串头尾的空格

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

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

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

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

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

设置 UITextView 的***输入长度

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

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

 

责任编辑:倪明 来源: 简书
相关推荐

2013-07-23 07:24:57

iOS开发学习iOS开发问题集锦

2021-07-16 07:57:35

SpringBootOpenFeign微服务

2012-12-24 13:23:26

iOS音频声效源码

2013-05-02 11:21:36

iOS开发流程

2020-11-24 08:15:09

Elasticsear面试分布式

2020-07-17 09:58:31

Python开发工具

2020-08-20 10:10:43

Prometheus架构监控

2018-09-12 21:25:15

iOSAppcrash

2013-04-12 15:59:33

2011-08-03 09:44:18

IOS开发 UITextFiel UITableVie

2015-09-16 09:57:41

swoolePHP程序员

2022-04-08 08:48:16

线上事故日志订阅者

2022-12-08 09:34:26

开发操作

2020-10-12 09:43:41

iOS 14漏洞苹果

2021-05-11 09:42:04

CI校验前端git config

2019-04-23 11:21:57

ERP系统管理信息化

2020-03-20 08:00:32

代码程序员追求

2021-09-29 09:07:22

Docker 日志容器

2023-10-31 08:01:48

Mybatis参数jdbcurl​

2021-01-22 05:35:19

Lvm模块Multipath
点赞
收藏

51CTO技术栈公众号