1

多くのビュー コントローラーを含むプロジェクトがあり、それらすべてに、タイル張りの背景画像を持つ親ビューが必要です。今、私はすることができました

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

UIView問題は、これが他のすべてのサブクラス化オブジェクト ( UIButtonsUILabelsなど)の背景も設定することです。UIView背景だけ変えるにはどうしたらいいですか?

4

3 に答える 3

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

MyTiledViewController のようなものを呼び出す UIViewController のサブクラスを作成し、init/viewDidLoad メソッドでこれを書くことができます...

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundPattern"]]];

編集: Gabriele Petronellaが私を打ち負かしたように見えますが、実際には同じです.

于 2013-04-18T16:25:20.207 に答える