[英]setting self of UIView background color
i'm trying to do this from inside the.m of a custom view class that is not being loaded from the XIB, but rather programmatically:我正在尝试从自定义视图 class 的.m 内部执行此操作,该视图不是从 XIB 加载的,而是以编程方式:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor redcolor];
}
return self;
}
i have the same result whether i put the background color in the initWithFrame or other methods.无论我将背景颜色放在 initWithFrame 还是其他方法中,我都有相同的结果。 the background color property doesn't take.
背景颜色属性不占用。 from the controller, which owns this custom view, i can set the background color fine, with:
从拥有此自定义视图的 controller 中,我可以很好地设置背景颜色,其中:
self.mycustomview.backgroundColor=[UIColor redcolor];
But I'd like to do this from within the custom view itself, keep stuff like this independent.但我想从自定义视图本身中做到这一点,保持这样的独立。 both the controller and the custom view import UIKit.
controller 和自定义视图导入 UIKit。
I also tried this, which was available from Code Sense:我也试过这个,可以从 Code Sense 获得:
self.View.backgroundColor=[UIColor redcolor];
but that doesn't work either.但这也不起作用。 i tried both
view
and View
here.我在这里尝试了
view
和View
。 I'm sure I'm overlooking something very obvious.我确定我忽略了一些非常明显的东西。
in the view controller i have this, and it works fine.在视图 controller 我有这个,它工作正常。 the custom view is called "mapButtons.h":
自定义视图称为“mapButtons.h”:
- (void)viewDidLoad
{
CGRect frame=CGRectMake(0, 0, 320, 460);
self.mapButtons=[[mapButtons alloc] initWithFrame:frame];
self.mapButtons.backgroundColor=[UIColor redColor];
[self.view addSubview:self.mapButtons];
the.h of the custom view is this:自定义视图的.h 是这样的:
#import <UIKit/UIKit.h>
@interface mapButtons : UIView
If your view is getting created from a XIB (ie you added it to some other view using Interface Builder), -initWithFrame:
is not going to get called.如果您的视图是从 XIB 创建的(即,您使用 Interface Builder 将其添加到其他视图),
-initWithFrame:
不会被调用。 An object being loaded from a XIB receives -initWithCoder:
instead.从 XIB 加载的 object 接收
-initWithCoder:
代替。 Try this:尝试这个:
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if(self)
{
self.backgroundColor = [UIColor redColor];
}
return self;
}
I have tested again and this is the full source for what I am doing that works我已经再次测试,这是我正在做的工作的完整来源
// MapButtons.h
#import <UIKit/UIKit.h>
// As a note you normally define class names starting with a capital letter
// but I did test this with mapButtons as you had it
@interface MapButtons : UIView
@end
// MapButtons.m
#import "mapButtons.h"
@implementation mapButtons
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor redColor];
}
return self;
}
@end
// TestAppDelegate.m
@implementation TestAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MapButtons *view = [[MapButtons alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:view];
[self.window makeKeyAndVisible];
return YES;
}
The fact that xcode is not auto completing is odd but I seem to have this issue intermittently so I have no real solution. xcode 不是自动完成的事实很奇怪,但我似乎间歇性地遇到这个问题,所以我没有真正的解决方案。 People sometimes suggest deleting the projects derived data and restarting xcode.
人们有时建议删除项目派生数据并重新启动 xcode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.