我有一个包含许多视图控制器的项目,我希望它们都具有带有平铺背景图像的父视图。现在我可以做
[[UIView appearance] setBackgroundColor:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern"]]];
问题是,这也会设置所有其他UIView
子类对象(UIButtons
、、UILabels
等)的背景。我能做些什么来改变UIView
背景?
我有一个包含许多视图控制器的项目,我希望它们都具有带有平铺背景图像的父视图。现在我可以做
[[UIView appearance] setBackgroundColor:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern"]]];
问题是,这也会设置所有其他UIView
子类对象(UIButtons
、、UILabels
等)的背景。我能做些什么来改变UIView
背景?
如果您appearance
在类级别使用代理,则别无选择。您将修改每个UIView
子类。
我没有看到许多其他子类化选项UIViewController
,仅更改该子类视图的外观,然后使所有其他UIViewController
子类成为第一个子类。
您可以在每种类型上创建类别,并添加一个设置角半径的 UIAppearance 选择器。
UIView+Appearance.h 文件:
#import <UIKit/UIKit.h>
@interface UIView (Appearance)
- (void)setCornerRadius:(CGFloat)cornerRadius UI_APPEARANCE_SELECTOR;
@end
UIView+Appearance.m 文件:
#import "UIView+Appearance.h"
@implementation UIView (Appearance)
- (void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
}
@end
AppDelegate.m 文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UIView appearanceWhenContainedIn:[ViewController class], nil] setCornerRadius:5.0];
return YES;
}
you could create a subclass of UIViewController calling it something like MyTiledViewController and in the init/viewDidLoad method write this...
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern"]]];
EDIT: looks like Gabriele Petronella beat me to it, same thing really.