記錄一下iOS開發中瑣碎的點點_4

1. UIViewController的兩個屬性presentedViewControllerpresentingViewController

他們分別是被present的控制器和正在presenting的控制器。好比說,控制器A和B,[A presentViewController B animated:YES completion:nil]; 那麼A相對於B就是presentingViewController,B相對於A是presentedViewController,即這個時候  app

B.presentingViewController = A;測試

A.presentedViewController = B;字體

這兩個屬性仍是頗有用的,能夠用來判斷當前控制器是被present出來的仍是push進來的.動畫

2. 經過子界面實現同個控制器下界面切換

咱們開發中,可能會遇到某個界面比較複雜,要進行多個界面的切換,若是把這些界面切換全都放在該界面中,控制器代碼很是臃腫不說,控制起來也比較麻煩,這個時候我建議用不一樣的控制器來表示不一樣的界面,而後將這些界面經過addChildViewController添加到控制器的子控制器中,而後經過系統提供的方法進行切換,至於這種方法怎麼用,你們看下官方文檔就知道了。ui

3. Masonry中的坑

注意!! 宏定義必需要放在 import 引入頭文件以前!this

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"

4. UINavigationBar背景透明和去底部邊線

//UINavigationBar背景透明
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //UINavigationBar去掉底部邊線
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

5. UITableView取消選中效果

之前有看到過別人家的app, 點擊tableview cell會跳轉到兩一個控制器, 當從下一個控制器返回時候, cell的選中效果會有一個漸變的取消選中的過程, 之前以爲挺好奇的,單也一直沒機會研究下, 其次公司只有我一個iOSer沒人交流..... 總之今天才看到, 原來就是一個屬性的事情.UITableViewController有一個專門的屬性用來能夠控制這個, 本身來實現的話, 實際上是在viewWillAppear:方法裏面調用tableView的方法取消選中效果, 以下:atom

//兩行代碼的事情, so easy
    NSIndexPath *selectedIndexpath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:selectedIndexpath animated:YES];

6. UIImage 調用drawInRect: 方法改變圖片大小後, 圖像失真問題

主要問題是, 默認是按照一倍圖來繪製的, 可是在retain屏上顯示就會致使失真, 解決就是, 建立 UIGraphics時候, 根據屏幕的分辨率來來建立, 以下:.net

UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);code

- (UIImage *)circleImageWithWidth:(double)width {
    
    CGSize size = CGSizeMake(width, width);
    
    // 開始圖形上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    
    // 得到圖形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 設置一個範圍
    CGRect rect = CGRectMake(0, 0, width, width);
    
    // 根據一個rect建立一個橢圓
    CGContextAddEllipseInRect(ctx, rect);
    
    // 裁剪
    CGContextClip(ctx);
    
    // 將原照片畫到圖形上下文
    [self drawInRect:rect];
    
    // 從上下文上獲取剪裁後的照片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 關閉上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

7. CGPoint轉換, 到另外一個view上面

- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;

8. tableViewCell 選中問題

首先, 我認可這個簡單的問題糾結了我很久, 今天偶然間發現瞭解決方案.orm

問題是, tableViewCell默認狀況下選中時候會有一個選中效果, 大概是背景灰色, 而我看到不少應用都是, 選中時候有選中的效果,而後自動的變成非選中狀態, 就這麼個簡單的問題, 前段時間也想了好久, 想到的方法是, 點擊時候選中, 而後在viewWillAppear:方法中取消選中狀態, 通常狀況下仍是能夠用的, 可是若是點擊cell不須要跳轉而是彈出對話框, 就有問題了, 今天發現的更好的方法就是, 在 didSelectRowAtIndexPath:方法中添加一行代碼, 以下:

// 選中一行後立刻取消選中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

這樣, cell被選中以後立刻回開始執行取消選中狀態的動畫. 好了 完美解決!!

9. 全國主要城市, 按照字母排序

http://download.csdn.net/detail/walden00/9611802

10. 修改UIBarButtonItem字體大小、顏色等屬性

[self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];

11. oc中 ({})的含義

12. UITextField 中, 右邊, 清除文本內容的按鈕

@property(nonatomic) UITextFieldViewMode clearButtonMode; // sets when the clear button shows up. default is UITextFieldViewModeNever

clearButtonMode = 1;

12. 測試代碼運行速度

CGFloat codeSpeedBlock (void (^block)(void)) {
    mach_timebase_info_data_t info;
    if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
    
    uint64_t start = mach_absolute_time ();
    block ();
    uint64_t end = mach_absolute_time ();
    uint64_t elapsed = end - start;
    
    uint64_t nanos = elapsed * info.numer / info.denom;
    
    return (CGFloat)nanos / NSEC_PER_SEC;
}
相關文章
相關標籤/搜索