0

I am using this method to change the backgrounds of my uiviewcontrollers. It generally works when I push a view controller.

However, if the viewcontroller is presented using

[self presentModalViewController:customViewController animated:YES];

then, this code doesnt work. Can anyone kindly suggest whats wrong ?

Code used:

To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as UINavigationBar+CustomBackground.m (it adds a custom category to UINavigationBar):

@implementation UINavigationBar (CustomBackground)

- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
Community
  • 1
  • 1
Ahsan
  • 2,964
  • 11
  • 53
  • 96
  • Have you tried with: [self.navigationController presentModalViewController:customViewController animated:YES]; – 3lvis Mar 14 '12 at 21:06

5 Answers5

2

If you are running on iOS 5, then drawRect: is no longer called

You will need to either use the UIAppearance or subclass UINavigationController and use that to change to you image.

A tutorial for UIAppearance can be found here

(drawRect: will still work on versions below iOS 5)

Amit Shah
  • 4,176
  • 2
  • 22
  • 26
  • My work around works perfect for view controllers that are pushed. For those that are presented modally, its causing this problem. Any clue why ? – Ahsan Mar 10 '12 at 02:44
  • 1
    I'm not sure, but if it is iOS 5 then it shouldn't be. Unless `setNeedsDisplay` is called. Although I don't know if that works. – Amit Shah Mar 10 '12 at 07:43
  • i dont think so, but appreciate the efforts. – Ahsan Mar 12 '12 at 23:03
0

not sure if this is it but have you tried

[self presentViewController:customViewController animated:YES completion:NULL]

And setting whatever modalViewStyle you deem appropriate for customViewController? According to the iOS documentation, presentModalViewController is deprecated, so you may have better luck using the above message call (especially since you only seem to be having this issue with modal view controllers)

Will Ayd
  • 6,767
  • 2
  • 36
  • 39
0

Try this ,

UIColor *image = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"imagename.png"]];

replace image name.

Sharon Nathaniel
  • 1,467
  • 20
  • 28
0

You have to create a navigation controller, set the root controller and present it.

e.g.

UIViewController *vc = [[UIViewController alloc] init];

UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentModalViewController:cntrol animated:YES];

apalvai
  • 587
  • 5
  • 11
0

The method you is to set the navigation bar image in navigation controller.

But The new view controller you present is not using navigation method. Instead, you use Modal view Controller.

I have two solution for you:

--- 1 -----

still use Modal View Controller, just add that image on the top of new view controller.

[self.view addSubview:theImage];

---- 2 -----

use

[self.navigationController pushViewController:customViewController animated:YES];

instead of

[self presentModalViewController:customViewController animated:YES];   

In this case, you are using navigation.

I will suggest the second one.

Doon
  • 97
  • 3