UITableView不显示背景图片
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用
UITableView
对象的backgroundView
属性?当然,如果您不设置框架,
backgroundView
将会调整大小,因此跳过第二行也应该不是问题。Why don't you use
backgroundView
property of theUITableView
object?Of course,
backgroundView
will be resized if you don't set the frame so skipping the second line should also not be a problem.你所拥有的应该可以解决。还是试试这个:
添加
viewDidLoad
What you have should probably work out. Still try this :
Add
in viewDidLoad