iOS開發(fā)小技巧

已經(jīng)很久沒有寫簡書了,主要是不知道要寫點(diǎn)什么。這次主要是記錄一些自己在開發(fā)過程中覺得有必要記載的一些小知識(shí),以免自己忘記。后期也會(huì)不定期的更新。下面就直接進(jìn)入主題吧!
  • pch文件的配置
    如果pch就在項(xiàng)目目錄中
    $(SRCROOT)/項(xiàng)目名/pch文件
    如果pch文件在項(xiàng)目中的一個(gè)文件夾內(nèi)
    $(SRCROOT)/項(xiàng)目名/文件夾名/pch文件

  • 利用TabBarController搭建項(xiàng)目基本結(jié)構(gòu)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addAllChildViewController];
}

/**
 * 添加所有子控制器
 */
- (void)addAllChildViewController {
   
}

/**
 * 設(shè)置子控制器
 */
- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.title = title;
    vc.tabBarItem.image = GetImage(imageName);
    vc.tabBarItem.selectedImage = [GetImage(selectedImageName) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:nav];
}
  • 設(shè)置tabBarItem 文字的顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBACOLOR(81, 81, 81, 1.0)} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : RGBACOLOR(18, 150, 219, 1.0)} forState:UIControlStateSelected];
  • Masonry 動(dòng)畫
重點(diǎn):[view.superview layoutIfNeeded];
示例:
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(400);
    make.left.mas_equalTo(100);
    make.size.mas_equalTo(CGSizeMake(100, 100));
}];

[view.superview layoutIfNeeded];//如果其約束還沒有生成的時(shí)候需要?jiǎng)赢嫷脑挘驼埾葟?qiáng)制刷新后才寫動(dòng)畫,否則所有沒生成的約束會(huì)直接跑動(dòng)畫

[UIView animateWithDuration:3 animations:^{
    [view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(200);
    }];
    [view.superview layoutIfNeeded];//強(qiáng)制繪制

}];
  • 字符編碼
    iOS中對字符串進(jìn)行UTF-8編碼:輸出str字符串的UTF-8格式
    [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    解碼:把str字符串以UTF-8規(guī)則進(jìn)行解碼
    [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  • 導(dǎo)航控制器奇幻的時(shí)候偶爾卡住的原因
原因:在RootViewController右劃返回手勢也可以響應(yīng),因?yàn)闆]有上一級(jí)頁面,導(dǎo)致整個(gè)程序頁面不響應(yīng)
解決辦法: 在導(dǎo)航控制器的viewDidLoad方法中設(shè)置代理
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}
然后實(shí)現(xiàn)代理方法,判斷是否是根控制器
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.viewControllers.firstObject == viewController) {
        self.interactivePopGestureRecognizer.enabled = false;
    }else {
        self.interactivePopGestureRecognizer.enabled = true;
    }
}

未完待續(xù)......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容