I'm trying to convert my app to the Swift language.
I have this line of code:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:textDictionary
forState:UIControlStateNormal];
How to convert it to Swift?
In Apple's docs, there is no such method.
You should be able to just translate the
Objective-C
syntax intoSwift
syntax.In swift the methods should be declared like this:
So you can try this:
I still have to figure out if this is the clean way to call a class method in
Swift
though.Hope this helps,
Update for iOS 9:
If you're targeting iOS 9+ (as of Xcode 7 b1), there is a new method in the
UIAppearance
protocol which does not use varargs:Which can be used like so:
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 fromUIView
):—
Just be sure to
#import "UIAppearance+Swift.h"
in your bridging header.Then, to call from Swift (for example):
Swift 4: iOS 9+
This can be extended to any class that conforms to the UIAppearance protocol -- not just UIViews. So here's a more generic version:
UIAppearance+Swift.h
UIAppearance+Swift.m
ios 10 swift 3
You can use this:
Edit: appearanceWhenContainedIn was removed in Swift. This answer was for the Beta 5 to change the appearance of the text of all bar buttons.