Objective-c:查看背景图案

时间:2011-01-09 13:22:50

标签: objective-c view background

是否可以在视图中具有可以重复x或y或两者的模式(例如网格)?有点像css:

background-image:url(pattern.png);    background-repeat:重复; //或repeat-x,repeat-y,no repeat;

2 个答案:

答案 0 :(得分:7)

以下代码将平铺背景图像:

- (void) viewDidLoad {
    [[self view] setBackgroundColor: [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"background.png"]]];
    [super viewDidLoad];
}

答案 1 :(得分:4)

是。您可以将图像模式加载为颜色([NSColor withPatternImage:[NSImage imageNamed:@"pattern"]]),然后像常规颜色一样绘制它:

- (void)drawRect:(NSRect)dirtyRect {
    ...
    // Load the image through NSImage and set it as the current color.
    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern"]] set];

    // Fill the entire view with the image.
    [NSBezierPath fillRect:[self bounds]];
    ...
}

此代码将在整个视图上重复该模式,但您只需稍加修改就可以使用x-repeat或y-repeat。


查看NSColor Class ReferenceNSView Class Reference了解详情。