UITableViewCell
具有透明背景,所以你已经看到了第一个控制器。实际上,我担心这没有解决方案。真遗憾我不能有一个固定的背景图片,而不必在每个控制器上设置背景图片。
UITableViewCell
具有透明背景,所以你已经看到了第一个控制器。实际上,我担心这没有解决方案。真遗憾我不能有一个固定的背景图片,而不必在每个控制器上设置背景图片。
我在一个iPhone应用程序中有这样的需求:根据用户的偏好设置页面背景图像。我处理的方法是将带有背景图像的UIImageView
作为子视图添加到视图中,就像这样 -
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
bgImageView.frame = self.view.bounds;
[self.view addSubview:bgImageView];
[self.view sendSubviewToBack:bgImageView];
我克隆了你的Github存储库,并在两个视图控制器的viewDidLoad
中添加了上述代码片段。我还在同一方法中添加了以下代码行 -
self.tableView.opaque = NO;
didFinishLaunchingWithOptions
中注释掉设置背景颜色的代码。这些更改后,在视图控制器之间导航时的伪像已经消失。我测试了iPhone Retina(3.5英寸)和iPhone Retina(4英寸)模拟器。
为什么在从Storyboard导航到ViewController
并返回时会出现伪像,需要进行一些调查。我的建议可能适用于您的要求,但是您可以尝试将其作为解决方案。
P.S. 该方法需要对自动布局约束进行一些微调。
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"MainBackground.png"]]];
并在每个屏幕的viewDidLoad方法中添加以下行:
[self.view setBackgroundColor:[UIColor clearColor]];
我没有找到一种全局设置的方法。不过,对于静态/固定图像(而不是当设置backgroundColor属性时得到的移动图像),对每个屏幕使用backgroundView
属性可能会很有用。
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
我通过创建一个UtilTableViewController来实现自己的需求,包括主题和自定义功能,将代码放在那里,然后再对所有视图进行子类化。它不是全局设置的图像,但我只需要设置一次,所有的TableViews都将使用它。
晚点发布...
如果您正在使用NavigationController,您可以尝试覆盖TopViewController的“get”部分,自动将BackGroundColor设置为您的图像。抱歉,我们使用Xamarin将其从C#转换为Objective C(不确定具体语法)。在C#中,它将在您的NavigationController类中看起来像这样。
public override UIViewController TopViewController
{
get
{
if (base.TopViewController.View.BackgroundColor != "Your Image")
{
base.TopViewController.View.BackgroundColor = "Your Image";
}
return base.TopViewController;
}
}
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
windowBackground=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background_window.png"]];
windowBackground.frame=CGRectMake(0, 0, 320, 568);
[window addSubview:windowBackground];
[windowBackground release];
window.frame = CGRectMake(0, 0, window.frame.size.width,568);
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
在每个视图控制器的viewDidLoad方法中添加此代码。
self.view.backgroundColor = [UIColor clearColor];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"YourImageName.png"]];
self.window.backgroundColor = background;
您可以通过以下代码设置背景图像...我们可以将此代码放在viewcontroller.m文件的viewdidload方法中
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];
appearanceWhenContainedIn:
的appearance
方法吗? - PopeyeappearanceWhenContainedIn:
可以让你为所有包含在UIViewController
类中的UITableViews
设置外观,例如backgroundColor:
。或者,你可以使用普通的外观方法来设置应用程序中每个UIView
的背景图像backgroundImage:
。 - PopeyeappearanceWhenContainedIn:
为表视图设置背景图片,那与在每个视图控制器中设置背景图片是一样的效果。 - Fred Collins