开发者

set background image for entire iPhone / iPad app

I have a sing开发者_开发问答le image I want as the background for my app no matter what viewcontroller they are on - how do you accomplish this?


Here's how you set a background to an image:

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

Edit: To write up what Felixyz said (and thanks to Manni), do this in your delegate:

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

And in each view you want to have the image, do this:

self.view.backgroundColor = [UIColor clearColor];


Depends on what sort of interface you have. Tabbed? Navigation based? But the general answer is: add a UIImageView to your UIWindow before/below your main view. Then make every view handled by your main view controller have a transparent background. Hard to give more specific advice without knowing if you use IB or not, or what your view hierarchy looks like.


In my app, I set a default background color. Maybe you can do this with you background image:

1.: Set the background color of your UIWindow in your AppDelegate:

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

2.: And now, make all other views transparent:

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


In your AppDelegate in

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

Add this line :

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

After you just have to set your views backgrounds to [UIColor clearColor];


I am not sure of the performance implications, but I needed to accomplish something similar, and ended up using a UIImageView which works well (in C#, but works the same in 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);

This doesn't handle orientation changes, but in my case, allowed me to add motion effects to the backdrop (such as parallax).


Your background is a property of any View-inheriting object. Labels, Buttons, Controllers, and the app window, for example, all have backgrounds. If you want it to be completely a bg for the entire app you must climb the path in your controllers to find the very "top" (bottom-viewed) view, and set its background to be the image you desire.


just call this assignbackground in 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)
    }


I usually use this function to avoid overlaps with navigation bar on 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜