(持續(xù)更新中……更新 SWTableViewCell 和 SVPullToRefresh 遇到的兩個(gè)小問(wèn)題)
在做自己的第一個(gè) iOS app,一路遇到不少困難,好在靠 Google 和 StackOverflow 都解決了,自己也不知道是否是 best practice。
隱藏 Tab bar
在以 Tab bar 劃分模塊的 app 中有些非一級(jí)界面是不需要底部的標(biāo)簽欄的,只需要在該 ViewController 的viewWillAppear:中加入設(shè)置標(biāo)簽欄隱藏的語(yǔ)句:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tabBarController.tabBar.hidden = YES;
}
但是,更好的作法是在 push 一個(gè) ViewController 之前,將其屬性hidesBottomBarWhenPushed設(shè)置為YES:
SomeViewController *svc = [SomeViewController new];
svc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:svc animated:YES];
計(jì)算 UIScrollView 的 ContentSize
有些 UIScrollView 的內(nèi)容是動(dòng)態(tài)增減的,這就需要重新計(jì)算 ContentSize,在改變內(nèi)容后增加以下代碼:
-(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);
}
貌似必須要在計(jì)算前最好執(zhí)行layoutIfNeeded,否則有些 sub view 還沒(méi)有布局好。
計(jì)算多行文本的高度
UILabel 和 UITextView 可以顯示多行的文本,如果字符串是動(dòng)態(tài)獲取的話就需要計(jì)算整個(gè)文本的高度了(寬度一般是固定的),這時(shí)就要用到boundingRectWithSize: options: attributes: context:這個(gè) API 了(iOS7新增的)。為了方便自己工程中調(diào)用,我封裝了一下:
+ (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];
}
去掉字符串頭尾的空格
對(duì)于 UITextField 中輸入的字符串往往都要進(jìn)行 trim 處理,需要用到以下代碼:
NSString *result = [self..nameTextField.text
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
監(jiān)聽(tīng) UITextView 的輸入,實(shí)時(shí)顯示字?jǐn)?shù)
首先要 conform(遵從?實(shí)現(xiàn)?) UITextViewDelegate,在textViewDidChange:中實(shí)現(xiàn)在 UILabel 中顯示當(dāng)前 UITextView 中的字?jǐn)?shù):
- (void)textViewDidChange:(UITextView *)textView {
_countLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)textView.text.length];
[self setNeedsDisplay];
}
設(shè)置 UITextView 的最大輸入長(zhǎng)度
實(shí)現(xiàn)UITextViewDelegate中的textView:shouldChangeTextInRange:方法:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// if (range.location >= kMaxTextLength) { 這樣會(huì)導(dǎo)致移動(dòng)光標(biāo)后再輸入最大長(zhǎng)度就失效
if(textView.text.length >= kMaxTextLength) {
return NO;
}
return YES;
}
SWTableViewCell 在滑動(dòng)的時(shí)候按鈕和 Cell 其他內(nèi)容重疊
用 SWTableViewCell 可以輕松實(shí)現(xiàn)滑動(dòng) Cell 并露出操作按鈕,但是遇到過(guò)操作按鈕和 Cell 上面的其他內(nèi)容重疊的情況,原因是被重疊的內(nèi)容沒(méi)有添加到contentView而是直接添加到了 Cell 上。
SVPullToRefreshControl 的上拉動(dòng)作只能執(zhí)行一次
用 SVPullToRefreshControl 可以實(shí)現(xiàn)對(duì) UITableView 的上拉加載更多的功能,但是如果發(fā)生只有第一次能夠成功觸發(fā)但是第二次就不行的情況,那么原因就是在加載完成時(shí)沒(méi)有停止刷新的動(dòng)畫(huà),只需要加入:
[self.tableView.infiniteScrollingView stopAnimating]; 就可以解決。
本文為博主原創(chuàng),轉(zhuǎn)載請(qǐng)保留鏈接,謝謝