UIStoryboard应用及常用背景图
我有一个 iOS 应用程序,它使用 UIStoryboard 来控制其流程。我希望在 UIStoryboard 中定义我的所有视图,以共享一个共同的背景。有没有一种方法可以做到这一点,而无需向每个视图添加 UIImageView 控件?
我在下面尝试过此操作,但它导致我的应用程序崩溃并出现堆栈溢出错误:
-(void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"MyBackgroundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
}
是否有更好的方法来执行此操作?在 iOS 应用程序中处理此类主题的最佳方法是什么?
I have an iOS application which uses an UIStoryboard to control its flow. I would like to have all my views defined in the UIStoryboard to all share a common background. Is there a way I can do this without having to add an UIImageView control to each View?
I have tried this below but it causes my application crash with a stack overflow error:
-(void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"MyBackgroundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
}
Is there a better way to do this? What is the best way to handle this kind of theming in iOS applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子类
UIViewController
并覆盖-viewDidLoad
以创建图像并将其设置为视图的背景。现在,将需要此背景图像的视图控制器设置为自定义视图控制器的子类,而不是UIViewController
。Subclass
UIViewController
and override-viewDidLoad
to create your image and set it as the background of the view. Now make the view controllers that require this background image subclasses of your custom view controller instead ofUIViewController
.在你的 ViewDidLoad: 中
应该可以解决问题。
In your ViewDidLoad:
Should do the trick.
事实证明我上面发布的代码运行得很好。我的解决方案与 Mark Adams 的建议相同:子类 UIViewController,覆盖 -viewDidLoad,创建并设置 imageView,并使用新的子类作为我的 viewController。
我想我可能无意中将我的新子类设置为 Interface Builder 中的错误控件,这导致我的初始解决方案无法正常工作。
It turns out that the code I posted above is working perfectly. My solution was the same as Mark Adams' suggestion: Subclass UIViewController, override -viewDidLoad, create and set the imageView, and use the new subclass as my viewController.
I think I may have inadvertently set my new subclass to an incorrect control in Interface Builder which caused my initial solution not to work correctly.