2018-08-15 iOS 奇技淫巧總結(jié)

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

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

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