Reputation: 5010
I'm trying to set a background image for the entire app following this suggestions: set background image for entire iPhone / iPad app
But in iOS 7 (don't know about other versions) it doesn't work well at all. I've created a simple repository so you can understand better what's is going on. There are some glitches in the transitions.
When you tap on a row in the first view, the second view is pushed into the navigation controller but there's a weird effect. It seems that the rows transparency played into this. Also the other problem is when you turn back to the previous view controller there's a subtle shadow of the view controller that is popped from the navigation stack. As I stated before you can get what I mean by running the simple Xcode project.
Repo: https://github.com/socksz/FixedBackgroundImage
Any ideas? I've already tried to set the background image for each controller but it isn't what I want because in that way the image "overlaps" the previous background image and it's not the desired effect.
Hope I explained well.
EDIT 1
It seems that the problem occurs because of the way iOS 7 manages the transitions between two view controllers. In you are in the second view controller and try to turn to the previous controller with the swipe gesture you can see that as you begin the gesture the first controller appears below the second controller (the controller you're seeing) and, since the UITableViewCell
s have transparent backgrounds, you already see the first controller. Actually I'm afraid that there's not a solution. What a pity that I cannot have a fixed background image without setting the background image on each controller.
Upvotes: 6
Views: 20819
Reputation: 11
you can set the background image through below code ... wew can put this code in viewdidload method in viewcontroller.m file
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]];
Upvotes: 0
Reputation: 631
I did not find a way to put this globally. However, and you will probably find this useful for static/fixed images (instead of the moving images you get when you set the backgroundColor property), Use the backgroundView
property for every screen.
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
I did it myself by creating a UtilTableViewController which does all theme and custom things I need it to do, putting this code there, then subclassing all my views. It's not a globally set image, but I only have to set it once and all of my TableViews will use it.
Upvotes: 2
Reputation: 11
Late post...
If you are using a NavigationController you might try overriding the TopViewController "get" portion to automatically set the BackGroundColor to your image. Appologies, we use Xamarin which converts from C# to objective C for us (not sure of the specific syntax). In C# it will look something like this within your NavigationController class.
public override UIViewController TopViewController
{
get
{
if (base.TopViewController.View.BackgroundColor != "Your Image")
{
base.TopViewController.View.BackgroundColor = "Your Image";
}
return base.TopViewController;
}
}
Upvotes: 1
Reputation: 3763
I had a requirement in an iPhone app to set the background image of a page based on a user's preferences. The way I dealt with it was to add a UIImageView
with the background image as a sub-view of the view, like so -
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
bgImageView.frame = self.view.bounds;
[self.view addSubview:bgImageView];
[self.view sendSubviewToBack:bgImageView];
I cloned your Github repository and added the above piece of code in viewDidLoad
of both the view controllers. I also added the following line of code in the same method -
self.tableView.opaque = NO;
I commented out the code in didFinishLaunchingWithOptions
where you set the background color. With these changes, the artifacts while navigating between view controllers are gone. I tested with iPhone Retina (3.5-inch) as well as iPhone Retina (4-inch) simulators.
The reason why the artifacts are seen while navigating to and from the ViewController
in the storyboard require some investigations. My suggestion may or may not work for your requirements, but, you can try this as a solution.
P.S. The method requires some tweaks to autolayout constraints.
Upvotes: 7
Reputation: 3928
Write this code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"YourImageName.png"]];
self.window.backgroundColor = background;
Upvotes: 0
Reputation: 2006
You Just have to write only one line in
appdelegate.m file's applicationdidFinishLaunchingWithOptions: method
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"MainBackground.png"]]];
and put below line in every screen's viewDidLoad method
[self.view setBackgroundColor:[UIColor clearColor]];
Upvotes: 5
Reputation: 800
Put this code in your appdelegate.m file applicationDidFinishLaunching method.
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
windowBackground=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background_window.png"]];
windowBackground.frame=CGRectMake(0, 0, 320, 568);
[window addSubview:windowBackground];
[windowBackground release];
window.frame = CGRectMake(0, 0, window.frame.size.width,568);
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
Add this code in every viewController class viewDidLoad method.
self.view.backgroundColor = [UIColor clearColor];
Upvotes: 1