在UIViewController(MonoTouch)中设置背景图像

问题描述:

我有一个名为HomeScreen的实现UIViewController的屏幕。我希望为这个屏幕使用背景图片。是否有可以覆盖的事件来在HomeScreen.cs文件中设置此背景图像?在UIViewController(MonoTouch)中设置背景图像

+2

UIViewController的犯规相当有BackgroundView /图像会员,你必须创建一个UImageView然后addSubView重复:HTTP:// *.com/questions/2991040/uiviewcontroller-with-background-image – Anuj

+0

谢谢。这不是一个重复,因为我的问题是与MonoTouch相关的。 –

+0

但是,如果您可以在MonoTouch中显示您的建议,我很乐意看到它。听起来这是我最好的选择。谢谢! –

尝试设置你的视图的BACKGROUNDCOLOR

myview.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("myimage.png")); 
+1

有没有什么办法可以让图像缩放到视图的大小,并仍然使用它作为背景颜色? – nhenrique

+0

@nhenrique我也想知道这个,你有没有找到解决方案? –

+0

@nhenrique我已经为你的问题添加了一个[新答案](https://*.com/a/47618726/4332059) –

尝试添加类似下面你MyViewNameViewController.cs:

public override void ViewDidLoad() 
{ 
    base.ViewDidLoad(); 
    this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("splash.png")); 
} 

我投了对其他的答案,但是今天IPhone具有不同大小和加载图像的正确方法是使用UIImage.FromBundle方法:

这里是项目中的断言目录:

enter image description here

要管理的图像:

enter image description here

public override void ViewDidLoad() 
{ 
    base.ViewDidLoad(); 

    // replace "name" with the desired name in the asset catalog 
    this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle("name")); 

}