1

我有一个包含许多视图控制器的项目,我希望它们都具有带有平铺背景图像的父视图。现在我可以做

[[UIView appearance] setBackgroundColor:
    [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern"]]];

问题是,这也会设置所有其他UIView子类对象(UIButtons、、UILabels等)的背景。我能做些什么来改变UIView背景?

4

3 回答 3

6

如果您appearance在类级别使用代理,则别无选择。您将修改每个UIView子类。

我没有看到许多其他子类化选项UIViewController,仅更改该子类视图的外观,然后使所有其他UIViewController子类成为第一个子类。

于 2013-04-18T16:24:49.170 回答
3

您可以在每种类型上创建类别,并添加一个设置角半径的 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;
}
于 2015-01-26T21:13:16.200 回答
2

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.

于 2013-04-18T16:25:20.207 回答