0

我对 IOS 很陌生;我通过drawRect方法做了一些渐变等,现在,如果我想实现它,我将它子类化到视图中。但我在想,有什么方法可以调用方法 drawRect 并让它从 ViewController 的子类中设置视图的背景?

布局是这样的:

背景颜色.h

#import <UIKit/UIKit.h>

@interface BackgroundColor : UIView

@end

背景颜色.m

#import "BackgroundColor"

@implementation BackgroundColor___Fuschia


- (void)drawRect:(CGRect)rect
{
...
}

@end

ViewControllerNav.h

#import <UIKit/UIKit.h>

@interface ViewControllerNav : UIViewController

@end

ViewControllerNav.m

@interface ViewControllerNav ()

@end

@implementation ViewControllerNav

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我假设我必须导入 BackgroundColor 并初始化方法还是?

(PS:我的 drawRect 方法使用 CGRect frame = rect;所以我需要通过我假设的视图大小?)

任何帮助都是有价值的

4

1 回答 1

1

To answer your original question, you never call drawRect: directly. You call

[self.view setNeedsDisplay];

and the system takes care of calling your drawRect: at the appropriate time.

于 2013-07-22T04:07:34.853 回答