【发布时间】:2011-06-22 09:02:05
【问题描述】:
我想要一张图片作为我的应用的背景,无论它们在什么视图控制器上 - 你是如何做到这一点的?
【问题讨论】:
标签: iphone objective-c ipad
我想要一张图片作为我的应用的背景,无论它们在什么视图控制器上 - 你是如何做到这一点的?
【问题讨论】:
标签: iphone objective-c ipad
取决于你有什么样的界面。标签?基于导航?但一般的答案是:在您的主视图之前/下方添加一个 UIImageView 到您的 UIWindow 。然后让你的主视图控制器处理的每个视图都有一个透明的背景。如果不知道您是否使用 IB,或者您的视图层次结构是什么样的,很难给出更具体的建议。
【讨论】:
以下是为图像设置背景的方法:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
编辑:要写下 Felixyz 所说的话(感谢 Manni),请在您的代表中执行此操作:
window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
在您想要拥有图像的每个视图中,执行以下操作:
self.view.backgroundColor = [UIColor clearColor];
【讨论】:
UINavigation 的应用程序中尝试了上述方法。这种方法的问题是在window.background 上设置图像正确设置图像但使其他视图清晰颜色不好因为在推送或弹出视图重叠时,您可以看到推送到堆栈中的视图。知道如何修复它
在我的应用程序中,我设置了默认背景颜色。也许你可以用你的背景图片来做这个:
1.:在 AppDelegate 中设置 UIWindow 的背景颜色:
window.backgroundColor = [UIColor myBackgroundGray]; // own Category
2.:现在,让所有其他视图透明:
self.view.backgroundColor = [UIColor clearColor]; // = transparent
【讨论】:
您的背景是任何继承视图的对象的属性。例如,标签、按钮、控制器和应用程序窗口都有背景。如果您希望它完全成为整个应用程序的背景,您必须爬上控制器中的路径以找到非常“顶部”(底部视图)的视图,并将其背景设置为您想要的图像。
【讨论】:
在你的 AppDelegate 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
添加这一行:
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
您只需将视图背景设置为[UIColor clearColor];
【讨论】:
我不确定性能影响,但我需要完成类似的事情,并最终使用了一个运行良好的 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);
这不处理方向变化,但在我的情况下,允许我向背景添加运动效果(例如视差)。
【讨论】:
我通常使用这个功能来避免与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];
}
【讨论】:
只需在viewDidLoad 中调用此assignbackground
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)
}
【讨论】: