Add a background image to TTWebController's UIToolbar - objective-c

I would like to add a nice background image to the UIToolbar of a subclass of Three20's TTWebController.
Since I don't mind all UIToolbars of my app sharing the same background, I tried using UIColor colorWithPatternImage in my style sheet and that doesn't seem to work (toolbar end up solid black).
I've also tried doing something like this:
#implementation UIToolbar (MyCustomToolbarBG)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: kBackgroundImage];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
with little success..
I certainely don't want to copy and paste the entire TTWebController code just to make my own class out of it since I actually don't modify anything else, but I don't really see anything else I can do. Anyone have a suggestion?

Ok, found my way like this: I subclassed the TTWebController to subclass the loadView: method.
-(void)loadView{
[super loadView];
[_toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed: kBackgroundImage]] autorelease] atIndex:0];
}
Better than nothing. Not extremely handy if they decide to change anything about the protected attribute _toolbar though...

Related

UITabBar unselected icon tint

I am trying to change the color of my tab bar icons when the tabs are UNselected. Right now the color is default grey and I can change the color to whatever color I want for when it IS selected.
Apple's dev library said to change the image rendering to "original" instead of its default mode "template." I did that. then it says to use initWithTitle:image:selectedImage: I tried to do that as well but I think that's where I messed up. I wrote this in my viewcontroller.m file. What's wrong here?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
stat = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
(instancetype)initWithTitle:(NSString *)nil image:(UIImage *)stat selectedImage:(UIImage *)stat;
}
The problem is that you are using the same UIImage with UIImageRenderingModeAlwaysOriginal in both places.
Your code should look something like
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
UIImage *statAlwaysOriginal = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:statAlwaysOriginal selectedImage:stat];
The other thing is that there are some actual syntax errors in your post (in the UITabBarItem initialization, but I suspect you just pasted it incorrectly.

iOS: Background image in UIImagePickerController is inconsistent with the rest of the app

I'm using the following code to set the navbar's background:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"tile.png"] forBarMetrics:UIBarMetricsDefault];
This results in a nice tiled background such as you see here:
However, when I open up a UIImagePickerController, the background is warped somehow and we end up with something like this:
Anyone have suggestions on how to fix it?
Stretching instead of tiling: to Stop That
Create a class of type UINavigationBar
Create your custom UINavigationBar subclass.
Comment initwithFrameMethod
Add method DrawRect:
(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"bluebackground.jpeg"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
4.in AppDelegate.m, AppliocationdidFinishLaunching
Write
[self.navigationbar setValue:[[customBar alloc]init] forKeyPath:#"navigationBar"];
Adding Image By this way will not stretch the image
Refer this Link:
http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
http://designm.ag/tutorials/designing-a-custom-iphone-app-navigation-bar/
Hope this will help you.

UIGraphicsGetImageFromCurrentImageContext() retain cause crash

This is a weird one, but I think I'm just missing something simple.
I have an Objective C class called Theme.
Themes load all the art for my game from files and stores them in a NSMutableArray.
When the app launches it reates a new Theme object.
Part of the loading process does something like this:
UIImage *image = [self mergeImage: bg toImage: overlay];
[imageArray addObject: image];
I take a background image and place the overlay on top of it, creating a new UIImage, by using this code:
- (UIImage *)mergeImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
//[resultingImage retain];
UIGraphicsEndImageContext();
return resultingImage;
}
All of this happens in the init function of Theme.
Here's the problem, when I call [[Theme alloc] init] from
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The app crashes a second later, leaving me with no stack or anything(I figure it screws up some memory and crashes later when it uses it again or something).
Here's the weird part. If I don't add the newly merged image to the imageArray, it never crashes. I don't have the image anymore, but there's no crash. Also, if I don't add the image to the array, but I retain it in the mergeImage function, it does crash, which makes me think that retaining this newly-made image is causing the problem.
Weirder still, if I don't call mergeImage until later on in the app, outside of teh [Theme init] and didFinishLaunchingWithOptions, it's OK.
Any idea why? What's wrong with retaining this image when the app first launches?
Try this Below Code:
UIGraphicsBeginImageContext(drawImage.bounds.size);
[drawImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage
Thanks..!

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

Optionally set navigationbar background image

I have the need to draw a background image or set a tint color on a navigation bar, but I also need the option to have the navigation bar appear as it normally would. I'm currently using a category to support If my app does not specify a background image, what can I do instead to ensure the drawRect method does it normally would do?
I.E. -
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
if(hasImage){
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://myimageurl.com/img.jpg"]]];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}else{
??????
}
}
#end
I actually ended up doing something entirely different and I'm wondering why nobody hasn't discovered this before. One of the approaches I've seen in the course of my Googling on the subject was simply adding an image as a subview to the UINavigationBar. The only problem was this made the buttons in the bar not clickable. The fix was to disable user interaction on the image.
myUIImageView.userInteractionEnabled = NO;
[myNavController.navigationBar addSubview:myUIImageView];
[myNavController.navigationBar sendSubviewToBack:myUIImageView];
With that, everything looks/works great and I don't have to override the drawRect method with a category, swizzle methods or any of that funky stuff. Simple and clean.
Theoretically you could do this by subclassing UINavigationBar overriding only the drawRect: method, and then calling [super drawRect:rect] when you want to use the default behavior.
But I don't believe you can in practice because you don't instantiate the UINavigationBar directly.
Solving this is possible but nontrivial, since the category method "replaces" the original method rather than subclassing it from the runtime's standpoint. This is why just using, say, super, won't work.
You should check out this post on "supersequent" implementation: http://cocoawithlove.com/2008/03/supersequent-implementation.html
(That link and some other related ideas are in the answer to this question: Using Super in an Objective C Category? )