UIStoryboard应用及常用背景图

发布于 2024-12-26 10:05:09 字数 515 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

不一样的天空 2025-01-02 10:05:09

子类 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 of UIViewController.

你如我软肋 2025-01-02 10:05:09

在你的 ViewDidLoad: 中

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:@"image.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imageView];

应该可以解决问题。

In your ViewDidLoad:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:@"image.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imageView];

Should do the trick.

拒绝两难 2025-01-02 10:05:09

事实证明我上面发布的代码运行得很好。我的解决方案与 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.

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