轉(zhuǎn)載,忘記出處了...
- 將 view 的所有子視圖移除
// 用 makeObjectsPerformSelector
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
- 數(shù)組的二分法查找, 時(shí)間復(fù)雜度是 O(log n)
NSArray *arr = @[@"21", @"12", @"13", @"14", @"25", @"26", @"27", @"28", @"29"];
NSInteger index = [arr indexOfObject:@"14"
inSortedRange:NSMakeRange(0, [arr count])
options:NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return NSOrderedDescending;
}else if ([obj1 integerValue] < [obj2 integerValue]) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"index = %ld", index);
- 使用 NSAttributedString 設(shè)置不同大小字體時(shí), 使兩種字體都垂直居中, 其中 a 是大號字體, b 是小號字體
[mark addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (a - b)) range:NSMakeRange(0, name.length)];
- 老項(xiàng)目沒有進(jìn)行字體適配,導(dǎo)致有些文本在 iPhone5 機(jī)型上無法顯示完整,這里寫了一個(gè) UIFont 的分類,用 runtime 對 UIFont 的
systemFontOfSize:方法進(jìn)行了替換,幾行代碼基本上搞定了老項(xiàng)目存在的問題;
#import "UIFont+NNSize.h"
#import <objc/runtime.h>
@implementation UIFont (NNSize)
+ (void)load {
Method newMethod = class_getClassMethod([self class], @selector(nnCustomSystemFontOfSize:));
Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
method_exchangeImplementations(newMethod, method);
}
+ (UIFont *)nnCustomSystemFontOfSize:(CGFloat)fontSize {
UIFont *newFont = nil;
if ([[UIScreen mainScreen] bounds].size.height < 667.0) {
newFont = [UIFont nnCustomSystemFontOfSize:fontSize * 0.9];
} else {
newFont = [UIFont nnCustomSystemFontOfSize:fontSize];
}
return newFont;
}
@end
- 隱藏導(dǎo)航欄下邊的灰色線條,直接在自定義的 UINavigationController 類里調(diào)用下邊的方法
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
比如:[self findHairlineImageViewUnder:self.navigationBar].hidden = YES;
- 動(dòng)態(tài)修改狀態(tài)欄的顏色
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.offset = scrollView.contentOffset.y;
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
if (self.offset <= -200) {
return UIStatusBarStyleDefault;
}
return UIStatusBarStyleLightContent;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
return UIStatusBarAnimationFade;
}
- 監(jiān)聽界面滾動(dòng),淡入淡出更改電池條顏色
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
if (self.collectionView.contentOffset.y <= -200) {
return UIStatusBarStyleLightContent;
}
return UIStatusBarStyleDefault;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
return UIStatusBarAnimationFade;
}
- 判斷控件是不是指定視圖的子視圖
BOOL isView = [textView isDescendantOfView:self.view];
- 修改 UITextField 中 Placeholder 的文字顏色
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
- 防止 UIScrollView 手勢覆蓋側(cè)滑手勢
[scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];