iOS-一些實(shí)用的小技巧

1. 全局隱藏tabbar黑色分割線

UITabBar.appearance().shadowImage = UIImage(named: "tabbarshadow")
UITabBar.appearance().backgroundImage = UIImage(named: "tabbarshadow")

2. 全局修改UITextField/UITextView光標(biāo)顏色

//修改所有的UITextField 和 UITextView的光標(biāo)顏色
UITextField.appearance().tintColor = kTYMainColor
UITextView.appearance().tintColor = kTYMainColor

3. 獲取UISearchBar中的輸入框

1.遍歷子視圖

UITextField *searchField = (UITextField *)[self subViewOfClassName:@"UISearchBarTextField" superView:self.mySearchBar];

/**
* @className:要查找的對(duì)象的類名
* @className:父視圖
*/
- (UIView *)subViewOfClassName:(NSString *)className superView:(UIView *)superView {
    for (UIView *subView in superView.subviews) {
        if([NSStringFromClass(subView.class) isEqualToString:className]){
            return subView;
        }
        UIView *resultFound = [self subViewOfClassName:className superView:subView];
        if(resultFound){
            return resultFound;
        }
    }
    return nil;
}

2.使用謂詞

- (UITextField *)kk_textField {
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
        return [candidateView isMemberOfClass:NSClassFromString(@"UISearchBarTextField")];
    }];
    return [self.subviews.firstObject.subviews filteredArrayUsingPredicate:predicate].lastObject;
}

3.KVC獲取

UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];

4. 解決子視圖響應(yīng)父視圖手勢(shì)

  1. 遵循代理<UIGestureRecognizerDelegate>
  2. 設(shè)置代理
UITapGestureRecognizer *dissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
dissTap.delegate = self;
[self addGestureRecognizer:dissTap];
  1. 實(shí)現(xiàn)代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  if ([touch.view isMemberOfClass:[self class]]) {
      return YES;
  }
  return NO;
}

5. 對(duì)UIView切指定圓角

/**
 切指定圓角

 @param corners 指定位置
 @param clipRadii 圓角尺寸
 */
- (void)clipByRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)clipRadii {
    //使用layout進(jìn)行布局,此時(shí)如果畫圓角會(huì)因self.bounds不正常,導(dǎo)致無法顯示圓角,所以需要提前更新UI,然后再畫圓角
    [self setNeedsLayout];
    [self layoutIfNeeded];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:clipRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = path.CGPath;   // 軌跡
    self.layer.mask = maskLayer;
}

6. 標(biāo)記廢棄方法名和屬性

// 廢棄屬性
@property (nonatomic, copy, nonnull) void(^tapBlock)(QGGDataAipTargetListItem *item) __attribute__((deprecated("廢棄")));
// 廢棄方法
- (id)initWithFrame:(CGRect)frame DEPRECATED_MSG_ATTRIBUTE("使用 `-initWithItems:` 代替.");

// 還有其他的
__deprecated_msg("廢棄了")
__attribute__((deprecated("廢棄了")))

7. 自定義NSLog

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

相關(guān)閱讀更多精彩內(nèi)容

  • { 11、核心動(dòng)畫 需要簽協(xié)議,但是系統(tǒng)幫簽好 一、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動(dòng)畫對(duì)象 CAB...
    CYC666閱讀 1,701評(píng)論 2 4
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,288評(píng)論 4 61
  • 我知道你瀟灑, 知道你追求多樣的人生, 也知道你可以駕馭各種風(fēng)格。 只是,別在夜里喝酒。 這樣傷身,而且你會(huì)哭泣。
    贏葉閱讀 143評(píng)論 0 1
  • 今天是平安夜,據(jù)說,平安夜的晚上要吃一個(gè)蘋果以保平安。我不知道一定要吃蘋果的原因是什么,或許只是單純的“平”字諧音...
    堅(jiān)持的考拉閱讀 324評(píng)論 0 0
  • 開發(fā)一款VR彈球游戲(下) 作者按:感謝大家對(duì)我的支持,甚至有出版社的主任跟我聯(lián)系要出書,尤為受寵若驚。這個(gè)系列的...
    養(yǎng)薛定諤的貓閱讀 1,977評(píng)論 6 13

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