设置自己的 UIView 背景颜色 [英] setting self of UIView background color

查看:36
本文介绍了设置自己的 UIView 背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从自定义视图类的 .m 内部执行此操作,该类不是从 XIB 加载,而是以编程方式加载:

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:

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    self.backgroundColor=[UIColor redcolor];
}
return self;
}

无论我将背景颜色放入 initWithFrame 还是其他方法,我都会得到相同的结果.背景颜色属性不带.从拥有此自定义视图的控制器中,我可以很好地设置背景颜色:

i have the same result whether i put the background color in the initWithFrame or other methods. the background color property doesn't take. from the controller, which owns this custom view, i can set the background color fine, with:

self.mycustomview.backgroundColor=[UIColor redcolor];

但我想从自定义视图本身中执行此操作,保持这样的独立性.控制器和自定义视图都导入 UIKit.

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.

我也试过这个,可以从 Code Sense 获得:

I also tried this, which was available from Code Sense:

    self.View.backgroundColor=[UIColor redcolor];

但这也不起作用.我在这里尝试了 viewView .我确定我忽略了一些非常明显的东西.

but that doesn't work either. i tried both view and View here. I'm sure I'm overlooking something very obvious.

在视图控制器中我有这个,它工作正常.自定义视图称为mapButtons.h":

in the view controller i have this, and it works fine. the custom view is called "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];

自定义视图的 .h 是这样的:

the .h of the custom view is this:

#import <UIKit/UIKit.h>

@interface mapButtons : UIView

推荐答案

我再次测试,这是我正在做的工作的完整来源

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;
}

xcode 不是自动完成的事实很奇怪,但我似乎间歇性地遇到这个问题,所以我没有真正的解决方案.人们有时会建议删除项目派生数据并重新启动 xcode.

The fact that xcode is not auto completing is odd but I seem to have this issue intermittently so I have no real solution. People sometimes suggest deleting the projects derived data and restarting xcode.

这篇关于设置自己的 UIView 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆