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 UIAppearance 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
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIView (UIViewAppearance_Swift)
// appearanceWhenContainedIn: is not available in Swift. This fixes that.
+ (instancetype)my_appearanceWhenContainedIn:(Class<UIAppearanceContainer>)containerClass;
@end
NS_ASSUME_NONNULL_END

// UIAppearance+Swift.m
#import "UIAppearance+Swift.h"
@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):

# Swift 2.x:
UITextField.my_appearanceWhenContainedIn(MyViewController.self).keyboardAppearance = .Light

# Swift 3.x:
UITextField.my_appearanceWhenContained(in: MyViewController.self).keyboardAppearance = .light

'AppearanceWhenContainedIn' is deprecated

It's up the people who write this CocoaPod to keep it up to date; contact them. Meanwhile, this is not your code, so do nothing. The code will work just fine for now, so no action is called for.

iOS – UIAppearance appearanceWhenContainedIn issues

Found out the solution to my problem:

Subclass the MFMessageComposeViewController

In the init method set the backgroundImage of the navigationBar to nil

Voilá!

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
// Custom initialization
[self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

}
return self;
}

How do I set Section attributes in UITableView?

Edit: Best Current Solution for iOS7+

Implement a tableView method in your UITableViewDelegate:

   func tableView(tableView: UITableView, willDisplayHeaderView view: UIView,
forSection section: Int) {

// Background color
view.tintColor = UIColor.blackColor()

// Text Color/Font
let header = view as UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.greenColor()
header.textLabel.font = my_font // put the font you want here...
}

ORIGINAL POST BELOW (outdated hacky solution)

Investigation

Searching around scant Apple docs on the subject and SO Q&A, I could not figure out how to use UITraitCollection.

However, I seem to have found a possible hack in this post in SO.

I have tested it with an implementation below and it seems to work.

We need to take three steps to create an Objective-C class method to do the job. Using bridging headers, we can seamlessly call the method from Swift.

Add to Bridging Header

#import "BridgeAppearance.h"

Create BridgeAppearance.h

@interface BridgeAppearance : NSObject { }

+ (void)setFontForUITableHeaderFooters: (UIFont*)font;

@end

Create BridgeAppearance.m

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BridgeAppearance.h"

@implementation BridgeAppearance

+ (void)setFontForUITableHeaderFooters: (UIFont*)font
{
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil]
setFont: font];
}

@end


Related Topics



Leave a reply



Submit