自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS被坑集錦

移動開發(fā)
在做自己的第一個 iOS app,一路遇到不少困難,好在靠 Google 和 StackOverflow 都解決了,自己也不知道是否是 best practice。

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

隱藏 Tab bar

在以 Tab bar 劃分模塊的 app 中有些非一級界面是不需要底部的標(biāo)簽欄的,只需要在該 ViewController 的viewWillAppear:中加入設(shè)置標(biāo)簽欄隱藏的語句:

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

但是,更好的作法是在 push 一個 ViewController 之前,將其屬性hidesBottomBarWhenPushed設(shè)置為YES:

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

計算 UIScrollView 的 ContentSize

有些 UIScrollView 的內(nèi)容是動態(tài)增減的,這就需要重新計算 ContentSize,在改變內(nèi)容后增加以下代碼:

  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); 

貌似必須要在計算前***執(zhí)行l(wèi)ayoutIfNeeded,否則有些 sub view 還沒有布局好。

計算多行文本的高度

UILabel 和 UITextView 可以顯示多行的文本,如果字符串是動態(tài)獲取的話就需要計算整個文本的高度了(寬度一般是固定的),這時就要用到boundingRectWithSize: options: attributes: context:這個 API 了(iOS7新增的)。為了方便自己工程中調(diào)用,我封裝了一下:

  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]]; 

監(jiān)聽 UITextView 的輸入,實時顯示字?jǐn)?shù)

首先要 conform(遵從?實現(xiàn)?) UITextViewDelegate,在textViewDidChange:中實現(xiàn)在 UILabel 中顯示當(dāng)前 UITextView 中的字?jǐn)?shù):

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

設(shè)置 UITextView 的***輸入長度

實現(xiàn)UITextViewDelegate中的textView:shouldChangeTextInRange:方法:

  1. -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 
  2.    // if (range.location >= kMaxTextLength) { 這樣會導(dǎo)致移動光標(biāo)后再輸入***長度就失效 
  3.     if(textView.text.length >= kMaxTextLength) { 
  4.         return NO; 
  5.     } 
  6.  
  7.     return YES; 

 

責(zé)任編輯:倪明 來源: 簡書
相關(guān)推薦

2013-07-23 07:24:57

iOS開發(fā)學(xué)習(xí)iOS開發(fā)問題集錦

2021-07-16 07:57:35

SpringBootOpenFeign微服務(wù)

2013-05-02 11:21:36

iOS開發(fā)流程

2012-12-24 13:23:26

iOS音頻聲效源碼

2025-04-22 03:00:00

模型SpringAI

2020-11-24 08:15:09

Elasticsear面試分布式

2020-07-17 09:58:31

Python開發(fā)工具

2020-08-20 10:10:43

Prometheus架構(gòu)監(jiān)控

2022-12-08 09:34:26

開發(fā)操作

2022-04-08 08:48:16

線上事故日志訂閱者

2015-09-16 09:57:41

swoolePHP程序員

2024-08-21 08:22:33

2018-09-12 21:25:15

iOSAppcrash

2013-04-12 15:59:33

2011-08-03 09:44:18

IOS開發(fā) UITextFiel UITableVie

2021-01-22 05:35:19

Lvm模塊Multipath

2020-03-20 08:00:32

代碼程序員追求

2021-09-29 09:07:22

Docker 日志容器

2023-10-31 08:01:48

Mybatis參數(shù)jdbcurl?

2019-04-23 11:21:57

ERP系統(tǒng)管理信息化
點贊
收藏

51CTO技術(shù)棧公眾號