1. 全局隐藏tabbar黑色分割线
UITabBar.appearance().shadowImage = UIImage(named: "tabbarshadow")
UITabBar.appearance().backgroundImage = UIImage(named: "tabbarshadow")
2. 全局修改UITextField/UITextView光标颜色
//修改所有的UITextField 和 UITextView的光标颜色
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. 解决子视图响应父视图手势
- 遵循代理
- 设置代理
UITapGestureRecognizer *dissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
dissTap.delegate = self;
[self addGestureRecognizer:dissTap];
- 实现代理
- (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进行布局,此时如果画圆角会因self.bounds不正常,导致无法显示圆角,所以需要提前更新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. 标记废弃方法名和属性
// 废弃属性
@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__);