appearanceWhenContainedIn in Swift
博客专区 > asjoker 的博客 > 博客详情
appearanceWhenContainedIn in Swift
asjoker 发表于2年前
appearanceWhenContainedIn in Swift
  • 发表于 2年前
  • 阅读 21
  • 收藏 0
  • 点赞 2
  • 评论 0
摘要: 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 http://www.openradar.me/17302764).

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;
@end
// UIAppearance+Swift.m
@implementation UIView (UIViewAppearance_Swift)
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass {
    return [self appearanceWhenContainedIn:containerClass, nil];
}
@end

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
共有 人打赏支持
粉丝 11
博文 106
码字总数 78651
×
asjoker
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: