ios 将背景图像添加到视图控制器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18720156/
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
Adding Background Image into View Controller
提问by Saint Robson
I tried to use this code under in my app delegate in order to add a PNG image as a view controller's background :
我尝试在我的应用程序委托中使用此代码,以便将 PNG 图像添加为视图控制器的背景:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[self window] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
return YES;
}
but I have no luck with it... the view controller still has a white background. What's wrong with that code?
但我没有运气......视图控制器仍然有白色背景。那个代码有什么问题?
回答by hadaytullah
The accepted answer and Michael's answer will work, however, proper way is to use a UIImageView instead. It gives more control over resizing, scaling etc according to different screen sizes on devices. Here is the example;
接受的答案和迈克尔的答案将起作用,但是,正确的方法是改用 UIImageView 。根据设备上的不同屏幕尺寸,它可以更好地控制调整大小、缩放等。这是示例;
First create a UIImage.
首先创建一个 UIImage。
UIImage *backgroundImage = [UIImage imageNamed:@"iphone_skyline3.jpg"];
Second create a UIImageView. Set the frame size to the parent's (self) frame size. This is important as the frame size will vary on different devices. Stretching will occur depending on the image size. Next assign the image to the view.
其次创建一个 UIImageView。将帧大小设置为父(自己)的帧大小。这很重要,因为帧大小在不同设备上会有所不同。根据图像尺寸会发生拉伸。接下来将图像分配给视图。
UIImageView *backgroundImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
backgroundImageView.image=backgroundImage;
Finally, to keep the image behind all controls do the following. It is important if you are setting the image as a background for your app.
最后,为了将图像保留在所有控件后面,请执行以下操作。如果您将图像设置为应用程序的背景,这一点很重要。
[self.view insertSubview:backgroundImageView atIndex:0];
回答by Tarek Hallak
You need to set the background for your ViewController's view
您需要为 ViewController 的视图设置背景
In your ViewController init
or viewDidLoad
:
在您的 ViewControllerinit
或viewDidLoad
:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
回答by Esqarrouth
Here is how it is in swift:
这是它的快速方式:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}
回答by Paco Cardenal
Swift 4 version of the @hadaytullah answer with some improvements in image adjustments:
@hadaytullah 答案的 Swift 4 版本在图像调整方面有一些改进:
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImage.init(named: "yourImageNameHere")
let backgroundImageView = UIImageView.init(frame: self.view.frame)
backgroundImageView.image = backgroundImage
backgroundImageView.contentMode = .scaleAspectFill
backgroundImageView.alpha = 0.1
self.view.insertSubview(backgroundImageView, at: 0)
}
回答by Enes Karaosman
You could also write extension to UIViewController to use in multiple places
您还可以为 UIViewController 编写扩展以在多个地方使用
extension UIViewController {
func setBackgroundImage(imageName: String) {
let backgroundImage = UIImage(named: imageName)
let backgroundImageView = UIImageView(frame: self.view.frame)
backgroundImageView.image = backgroundImage
self.view.insertSubview(backgroundImageView, at: 0)
}
}
回答by Amr Angry
you may consider the following solution i use in swift 3.0 and tested in xcode 8
您可以考虑我在 swift 3.0 中使用并在 xcode 8 中测试的以下解决方案
// init and image with your pattern image
var bgUIImage : UIImage = UIImage(named: "yourIamgeName")!
let myInsets : UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
bgUIImage = bgUIImage.resizableImage(withCapInsets: myInsets)
self.view.backgroundColor = UIColor.init(patternImage:bgUIImage)
the result will be the following image
结果将是以下图像
回答by Michael M
In viewDidLoad, I use:
在 viewDidLoad 中,我使用:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageString.png"]];
Alternatively, if you want to do it in appDelegate, I think its possible to
或者,如果您想在 appDelegate 中执行此操作,我认为可以
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageString.png"]]];
Then in viewDidLoad set background to [UIColor clearColor] ?
然后在 viewDidLoad 中将 background 设置为 [UIColor clearColor] ?
回答by umakanta
If your view controller is having tableView/collection view the below code will suitable for you.
如果您的视图控制器具有 tableView/collection 视图,以下代码将适合您。
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]];
}
回答by Boris Nikolic
You can do it from the storyboard.
你可以从故事板中做到这一点。
Add UIImageView and just set your background image for the Image property in the Attribute Inspector.
添加 UIImageView 并在属性检查器中为 Image 属性设置背景图像。