6

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 UITableViewCells 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.

enter image description here

Community
  • 1
  • 1
Fred Collins
  • 5,004
  • 14
  • 62
  • 111
  • Have you looked at the `appearance` method `appearanceWhenContainedIn:`? – Popeye Dec 03 '13 at 16:16
  • @Popeye for which purpose? Can you give me more details? Thanks! – Fred Collins Dec 03 '13 at 17:22
  • Using `appearanceWhenContainedIn:` will allow you to set the appearance for lets say `backgroundColor:` for all `UITableViews` contained with in lets say `UIViewController class` either that or you can use the normal appearance methods to set the `backgroundImage:` the background for every `UIView` in your app. – Popeye Dec 03 '13 at 19:33
  • Thanks Popeye but it's not my problem. What I'm trying to achieve is have one fixed background image that stays below the tableview. If I set the background image for tableviews with `appearanceWhenContainedIn:` it's the same as setting the background image in each view controller. – Fred Collins Dec 04 '13 at 02:22

7 Answers7

7

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.

indyfromoz
  • 4,045
  • 26
  • 24
5

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]];
Haresh Ghatala
  • 1,996
  • 17
  • 25
2

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.

Velociround
  • 611
  • 7
  • 18
1

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];
Naveen kumar
  • 800
  • 1
  • 5
  • 23
1

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;
  }
}
Kirk
  • 11
  • 1
0

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;
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
0

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"]]];