2011-02-06 84 views

回答

88

这里是你如何设置一个背景图像:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]]; 

编辑:写上去Felixyz说什么(感谢曼尼),为此在您的委托:

window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]]; 

并在每个视图中您想要图像,请执行以下操作:

self.view.backgroundColor = [UIColor clearColor]; 
+7

我最终在主窗口笔尖上使用了UIImageView,并在我的视图上使用了UIColor clearColor。请注意,如上所述,使用colorWithPatternImage使我的内存使用从第一次加载应用程序时从1.7M跳到9.5M - colorWithPatternImage似乎只消耗了大量内存。 – Slee 2011-02-10 13:38:17

8

取决于你有什么样的接口。分页?基于导航?但一般的答案是:在你的主视图之前/之下添加一个UIImageView到你的UIWindow。然后让您的主视图控制器处理的每个视图都具有透明背景。很难给出更具体的建议,不知道你是否使用IB,或者你的视图层次结构是什么样子。

+1

导航基于调用这个assignbackground,从而视图控制器入栈和出栈的背景保持 – Slee 2011-02-06 21:46:25

+0

结合须藤的和曼尼的建议,让您轻松完成你所需要的:设置在窗口上使用backgroundColor作为图像(colorWithPatternImage :)并在导航控制器及其所有子控制器上使backgroundColor透明。你可能必须在它们上设置opaque = NO。请注意,这可能会影响动画过程中的性能,但在新设备上不应该是问题。 – Felixyz 2011-02-06 23:07:23

7

在我的应用程序,我设置了默认的背景颜色。也许你可以跟你的背景图片做:

1:设置您的UIWindow的背景颜色在你的AppDelegate:

window.backgroundColor = [UIColor myBackgroundGray]; // own Category 

2:现在,让所有其他视图透明:

self.view.backgroundColor = [UIColor clearColor]; // = transparent 
2

你的背景是任何View-继承对象的属性。例如,标签,按钮,控制器和应用程序窗口都具有背景。如果您希望它完全适用于整个应用程序,那么您必须爬过控制器中的路径才能找到“顶部”(底部查看)视图,并将其背景设置为您想要的图像。

7

在你的AppDelegate在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

加入这一行:

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

之后,你只需要你的意见的背景设置为[UIColor clearColor];

3

我不知道对性能的影响,但我需要完成类似的东西,并最终使用效果很好一个UIImageView(在C#中,但工作原理是一样的OBJ-C):

//Add the view controller first, to ensure proper order of views later. 
Window.RootViewController = new UIViewController(); 

//create backdrop image view 
var imageView = new UIImageView(Window.Bounds); 
imageView.Image = UIImage.FromBundle("backdrop.jpg"); 

//insert into window. 
Window.InsertSubview(imageView, 0); 

这不处理方向更改,但在我的情况下,允许我将运动效果添加到背景(如视差)。

0

我通常使用此功能,以避免与iphone导航栏重叠。

-(void)setImageBackground:(NSString*)imageName{ 
    UINavigationController* navigationController = [self navigationController]; 
    float height = navigationController.toolbar.frame.size.height; 
    CGSize size = self.view.frame.size; 
    size.height = size.height; 
    UIGraphicsBeginImageContext(size); 
    CGRect bounds = self.view.bounds; 
    bounds.origin.y = bounds.origin.y + height; 
    bounds.size.height = bounds.size.height-height; 
    [[UIImage imageNamed:imageName] drawInRect:bounds]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
} 
0

只是viewDidLoad

override func viewDidLoad() { 
assignbackground() 
} 

func assignbackground(){ 
     let background = UIImage(named: "background") 

     var imageview : UIImageView! 
     imageview = UIImageView(frame: view.bounds) 
     imageview.contentMode = UIViewContentMode.ScaleAspectFill 
     imageview.clipsToBounds = true 
     imageview.image = background 
     imageview.center = view.center 
     view.addSubview(imageview) 
     self.view.sendSubviewToBack(imageview) 
    }