iOS 按钮形状

需要解决的问题

iOS7之后,在 设置->通用->辅助功能->按钮形状 打开后,界面会从


iOS 按钮形状_第1张图片
IMG_3790.PNG

变成


iOS 按钮形状_第2张图片
IMG_3789.PNG
这样,除了返回按钮之外,按钮会默认加上下划线,tabbar button也会出现对应的阴影(selectionIndicatorImage)

解决办法

  • UITabbar
[UITabBar appearance].selectionIndicatorImage = [UIImage new];
  • UIButton 给UIButton添加了一个分类
 + (void)load {
    //只执行一次这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [UIButton changeFontBottomLine];
    });
}
#pragma mark - Change Font Bottom Line
 + (void)changeFontBottomLine {
    Class class = [self class];
    
    SEL originalSelector = @selector(setTitle:forState:);
    SEL swizzledSelector = @selector(CKSetTitle:forState:);
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
        
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}
 - (void)CKSetTitle:(NSString *)title forState:(UIControlState)state {
    [self CKSetTitle:title forState:state];
    NSDictionary *attrbiteDic = @{
                                  NSFontAttributeName:self.titleLabel.font,
                                  NSForegroundColorAttributeName:[self titleColorForState:state],
                                  NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]
                                  };
    
    self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:title attributes:attrbiteDic];
}

你可能感兴趣的:(iOS 按钮形状)