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:要查找的對象的類名
* @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)父視圖手勢
- 遵循代理
<UIGestureRecognizerDelegate>
- 設(shè)置代理
UITapGestureRecognizer *dissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
dissTap.delegate = self;
[self addGestureRecognizer:dissTap];
- 實(shí)現(xiàn)代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isMemberOfClass:[self class]]) {
return YES;
}
return NO;
}
5. 對UIView切指定圓角
/**
切指定圓角
@param corners 指定位置
@param clipRadii 圓角尺寸
*/
- (void)clipByRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)clipRadii {
//使用layout進(jìn)行布局,此時(shí)如果畫圓角會因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__);