我在 iPhone 屏幕的每一侧都有两张带有花朵的图像。我想将这些图像添加到 UITableView 背景。
截图
问题是 :如何以编程方式或使用 Storyboard 添加它?
我想在每个 iPhone 屏幕上使用这种花,可以吗?或者我应该在 Storyboard 的每个 ViewController 中添加它?
我做的代码
UIImageView *leftFlowersIView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 36, 455)];
[leftFlowersIView setImage:[UIImage imageNamed:@"flower_left.png"]];
[self.tableView.backgroundView addSubview:leftFlowersIView];
UIImageView *rightFlowersIView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 36, 0, 36, 455)];
[rightFlowersIView setImage:[UIImage imageNamed:@"flower_right.png"]];
[self.tableView.backgroundView addSubview:rightFlowersIView];
我收到一个空白背景。
[self.view addSubview:leftFlowersIView];
它有效,但是当我向下滚动时 - 背景也在滚动。
self.tableView.backgroundView = leftFlowersIView;
图像被拉伸(stretch)以填满屏幕。
在此先感谢,阿尔乔姆。
结果是
UIImageView *backgroundIView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
UIImageView *leftFlowersIView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 36, 455)];
[leftFlowersIView setImage:[UIImage imageNamed:@"flower_left.png"]];
[backgroundIView addSubview:leftFlowersIView];
UIImageView *rightFlowersIView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 36, 0, 36, 455)];
[rightFlowersIView setImage:[UIImage imageNamed:@"flower_right.png"]];
[backgroundIView addSubview:rightFlowersIView];
self.tableView.backgroundView = backgroundIView;
最佳答案
为此,您可以制作一个框架等于表格 View 框架的单个图像,然后只需添加以下代码:
tableview.backgroundColor = [UIColor colorWithPatternImage:yourimage];
关于ios - 如何将两个 UIImage 添加到 UITableView 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21109706/