加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 设置UIView背景颜色的自我

发布时间:2020-12-16 09:36:42 所属栏目:百科 来源:网络整理
导读:我试图从没有从XIB加载的自定义视图类的.m内部执行此操作,而是以编程方式执行此操作: - (id)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) { // Initialization code self.backgroundColor=[UIColor redcolor];}return self;}
我试图从没有从XIB加载的自定义视图类的.m内部执行此操作,而是以编程方式执行此操作:

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

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

我是否将背景颜色放在initWithFrame或其他方法中,我有相同的结果.背景颜色属性不需要.从拥有此自定义视图的控制器,我可以设置背景颜色,使用:

self.mycustomview.backgroundColor=[UIColor redcolor];

但是我想在自定义视图中做到这一点,保持这样的东西独立.控制器和自定义视图导入UIKit.

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

self.View.backgroundColor=[UIColor redcolor];

但这也不起作用.我试过这里查看和查看.我敢肯定我忽视了一些非常明显的事情.

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

- (void)viewDidLoad
{
CGRect frame=CGRectMake(0,320,460);
self.mapButtons=[[mapButtons alloc] initWithFrame:frame];
self.mapButtons.backgroundColor=[UIColor redColor];

[self.view addSubview:self.mapButtons];

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

#import <UIKit/UIKit.h>

@interface mapButtons : UIView

解决方法

我再次测试过,这是我正在做的事情的完整来源

// 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读