appearanceWhenContainedIn in Swift
博客专区 > asjoker 的博客 > 博客详情
appearanceWhenContainedIn in Swift
asjoker 发表于2年前
appearanceWhenContainedIn in Swift
  • 发表于 2年前
  • 阅读 21
  • 收藏 0
  • 点赞 2
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: appearanceWhenContainedIn in Swift

转载 appearanceWhenContainedIn in Swift

Update for iOS 9: If you're targeting iOS 9+ (as of Xcode 7 b1), there is a new method in the UIApperance protocol which does not use varargs:

static func appearanceWhenContainedInInstancesOfClasses(containerTypes: [AnyObject.Type]) -> Self

Which can be used like so:

UITextField.appearanceWhenContainedInInstancesOfClasses([MyViewController.self]).keyboardAppearance = .Light

If you still need to support iOS 8 or earlier, use the following original answer to this question.

For iOS 8 & 7: These methods are not available to Swift because Obj-C varargs methods are not compatible with Swift (see

I wrote a non-variadic workaround which works in Swift (I repeated the same method for UIBarItem, which doesn't descend from UIView):

// UIAppearance+Swift.h
@interface UIView (UIViewAppearance_Swift)
// appearanceWhenContainedIn: is not available in Swift. This fixes that.
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass;
// UIAppearance+Swift.m
@implementation UIView (UIViewAppearance_Swift)
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass {
    return [self appearanceWhenContainedIn:containerClass, nil];

Just be sure to #import "UIAppearance+Swift.h" in your bridging header.

Then, to call from Swift (for example):

UITextField.my_appearanceWhenContainedIn(MyViewController.self).keyboardAppearance = .Light
共有 人打赏支持
粉丝 10
博文 107
码字总数 79226
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型