xcode 使用 Storyboard 修复 iPhone 应用程序的背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9265635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:18:13  来源:igfitidea点击:

Fixed Background for iPhone app using Storyboard

iphoneobjective-cxcode

提问by resedasue

Can't seem to find a way to fix a graphic - a light graphic that would remain static as the user navigates from scene to scene. Have been combing forums for a few days, but most answers point to MainWindow.xib, which isn't generated for a storyboard project. Would appreciate any advice.

似乎无法找到修复图形的方法 - 当用户从场景导航到场景时,浅色图形将保持静止。几天来一直在梳理论坛,但大多数答案都指向 MainWindow.xib,它不是为故事板项目生成的。将不胜感激任何建议。

采纳答案by Max

You can try to manually add UIImageView to your window and then set 0 background transparency for all other views.

您可以尝试手动将 UIImageView 添加到您的窗口,然后为所有其他视图设置 0 背景透明度。

回答by resedasue

Here's what I was able to cobble together thanks to advice from @Max. Like I said, very new to this, so if this code needs tuning please leave a comment so I don't lead others astray.

由于@Max 的建议,这就是我能够拼凑起来的东西。就像我说的,这很新,所以如果这段代码需要调整,请留下评论,这样我就不会误导其他人。

Add the image to the app's main window in the AppDelegate's didFinishLaunchingWithOptions method :

在 AppDelegate 的 didFinishLaunchingWithOptions 方法中将图像添加到应用程序的主窗口:

UIImageView *myGraphic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myGraphic.png"]];
[self.window.rootViewController.view addSubview: myGraphic];
[self.window.rootViewController.view sendSubviewToBack: myGraphic];

Then, to make your views transparent so the background shows through - in your viewController class/es, add to viewDidLoad method:

然后,为了使您的视图透明以便背景显示出来 - 在您的 viewController 类/es 中,添加到 viewDidLoad 方法:

self.view.backgroundColor = [UIColor clearColor];

(And note if you try to decrease the transparency through the alpha property for the view via the Attributes Inspector, it will make everything transparent on that view -buttons, etc - that's why it needs to be done programmatically.)

(请注意,如果您尝试通过 Attributes Inspector 通过视图的 alpha 属性降低透明度,它将使该视图 - 按钮等上的所有内容都透明 - 这就是为什么需要以编程方式完成的原因。)

Sure, it's not thoroughly tested. But for now it's working and it's a thing of beauty. Thanks all.

当然,它没有经过彻底的测试。但就目前而言,它正在发挥作用,这是一件很美的事情。谢谢大家。

回答by Canopus

One way to do it -- it may not be the best -- can be that you subclass a UIView, and set the UIView instance in all of your viewControllers in the storyboard an instance of your cutomized UIView class, which contains your graphic as a background.

一种方法 - 它可能不是最好的 - 可以是您对 UIView 进行子类化,并在故事板中的所有 viewController 中设置 UIView 实例,将 UIView 实例设置为自定义 UIView 类的实例,其中包含您的图形作为背景。

回答by Matisse VerDuyn

If you're going to tackle this programmatically, within each view controller, set:

如果您要以编程方式解决此问题,请在每个视图控制器中设置:

[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

in viewDidLoad

viewDidLoad

I'm sure some tweaking would allow you to apply this principle throughout the program from the app delegate, or by using a singleton.

我敢肯定,一些调整将允许您通过应用程序委托或使用单例在整个程序中应用此原则。

Using this solution, though, you're still going to have to make some method call within each view controller where you want this style applied.

但是,使用此解决方案,您仍然需要在要应用此样式的每个视图控制器中进行一些方法调用。