The accepted answer is not correct. You can set properties on layers, however, you need to subclass the view and expose the layer properties through accessors.
To illustrate, I'll use just one property from the question, cornerRadius
:
Step 1:
Implement a UIButton subclass.
#import <UIKit/UIKit.h>
@interface MyRoundedCornerButton : UIButton
@end
Step 2:
Add a property tagged with UI_APPEARANCE_SELECTOR
.
#import <UIKit/UIKit.h>
@interface MyRoundedCornerButton : UIButton
@property (readwrite, nonatomic) CGFloat cornerRadius UI_APPEARANCE_SELECTOR;
@end
Step 3:
Implement the new class.
@implementation MyRoundedCornerButton
- (void)setCornerRadius:(CGFloat)cornerRadius
{
self.layer.cornerRadius = cornerRadius;
}
@end
Step 4:
Set the corner radius in the appearance proxy.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[MyRoundedCornerButton appearance].cornerRadius = 10.0;
...
}
Step 5:
Then in IB, (or wherever you define the creation of the View), set the custom view class to (or instantiate an instance of) MyRoundedCornerButton instead of UIButton.
Notes:
I've done this to apply an easily changeable gradient background throughout my app. In my case, all view controllers' root view use a custom class. This custom class provides a CAGradientLayer
through the +(Class)layerClass
method. Then, I expose the colors
and locations
properties of the underlying gradient layer using the UI_APPEARANCE_SELECTOR
tag. Setting it once on app initialization customizes the entire app. You could even expose the colors to the user to allow them to fully customize colors of various controls.