UITableView不显示背景图片

发布于 2024-11-16 11:58:01 字数 1719 浏览 1 评论 0原文

我有一个 uitableview,有 2 个单元格(自定义 tableviewcells)和一个登录按钮。我以编程方式制作了它们。现在我想在 2 个自定义单元格和按钮后面放置一个背景图像。我进入IB并将图像视图放在表格视图的顶部,然后在上面放一张我想要作为背景的图片。然后我进入 viewDidLoad 并输入这段代码 tblSimpleTable.backgroundColor = [UIColorclearColor]; 来清除表格视图背景颜色。

现在看起来像这样:

在此处输入图像描述

但背景没有出现。有谁知道如何解决这个问题?

谢谢。

viewDidLoad

  - (void)viewDidLoad {
 [super viewDidLoad];

self.dataSourceArray = [NSArray arrayWithObjects:
                        [NSDictionary dictionaryWithObjectsAndKeys:
                         @"UITextField", kSectionTitleKey,
                         @"TextFieldController.m: textFieldNormal", kSourceKey,
                         self.textFieldNormal, kViewKey,
                         nil],
                        nil];

UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame = CGRectMake(60,140,200,38); // position in the cell and set the size of the button
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
// add targets and actions
[loginButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:loginButton];

tblSimpleTable.backgroundColor = [UIColor clearColor];
//tblSimpleTable.backgroundView = nil; 

//UIImageView *imageView = [[UIImageView alloc] initWithImage:@"Background-Red.png"];
//imageView.frame = tblSimpleTable.frame;
//tblSimpleTable.backgroundView = imageView;

self.title = NSLocalizedString(@"TextFieldTitle", @"");

// we aren't editing any fields yet, it will be in edit when the user touches an edit field
self.editing = NO;
 }

I have a uitableview with 2 cells (custom tableviewcells) and a button for login. I made them programmatically. Now I want to put a background image behind the 2 custom cells and the button. I went into IB and put the image view on top of the tableview and then put a picture on it which I want as the background. Then I went into viewDidLoad and put this bit of code tblSimpleTable.backgroundColor = [UIColor clearColor]; to clear the tableviews background colour.

It now looks like this:

enter image description here

But the background does not appear. Does anyone know how to fix this?

Thanks.

viewDidLoad

  - (void)viewDidLoad {
 [super viewDidLoad];

self.dataSourceArray = [NSArray arrayWithObjects:
                        [NSDictionary dictionaryWithObjectsAndKeys:
                         @"UITextField", kSectionTitleKey,
                         @"TextFieldController.m: textFieldNormal", kSourceKey,
                         self.textFieldNormal, kViewKey,
                         nil],
                        nil];

UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame = CGRectMake(60,140,200,38); // position in the cell and set the size of the button
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
// add targets and actions
[loginButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:loginButton];

tblSimpleTable.backgroundColor = [UIColor clearColor];
//tblSimpleTable.backgroundView = nil; 

//UIImageView *imageView = [[UIImageView alloc] initWithImage:@"Background-Red.png"];
//imageView.frame = tblSimpleTable.frame;
//tblSimpleTable.backgroundView = imageView;

self.title = NSLocalizedString(@"TextFieldTitle", @"");

// we aren't editing any fields yet, it will be in edit when the user touches an edit field
self.editing = NO;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-11-23 11:58:01

为什么不使用 UITableView 对象的 backgroundView 属性?

UIImageView * imageView = [[UIImageView alloc] initWithImage:yourBackgroundImage];
imageView.frame = tblSimpleTable.frame;
tblSimpleTable.backgroundView = imageView;

当然,如果您不设置框架,backgroundView 将会调整大小,因此跳过第二行也应该不是问题。

Why don't you use backgroundView property of the UITableView object?

UIImageView * imageView = [[UIImageView alloc] initWithImage:yourBackgroundImage];
imageView.frame = tblSimpleTable.frame;
tblSimpleTable.backgroundView = imageView;

Of course, backgroundView will be resized if you don't set the frame so skipping the second line should also not be a problem.

何其悲哀 2024-11-23 11:58:01

你所拥有的应该可以解决。还是试试这个:
添加

tblSimpleTable.backgroundView = nil;  

viewDidLoad

What you have should probably work out. Still try this :
Add

tblSimpleTable.backgroundView = nil;  

in viewDidLoad

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文