How do I change the toolbar's background?
Why not just adjust the tintColor property? Or is changing the tint not good enough for your purposes?
There's one common technique, which is a giant hack, but it seems to work. Subclass UIToolbar and override -drawRect: and just have that draw whatever you want. You can then create instances of the subclass whenever you need a toolbar, and if you have a toolbar in a xib file then you can just select it and change the class to whatever your subclass is.
Related
This tutorial shows how to draw on a graphics context for the view using Quartz 2D:
http://www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D
But I want to draw on a UIButton, not on a view. How can I do that?
Thanks
Draw on UIView . Add the view as subview to your UIButton.
A button IS a UIView. It inherits from UIControl, which inherits from UIView.
So buttons have a drawRect method.
So you can do everything described in the article you linked on a button.
However, buttons are set up to do a lot of things for you, and overriding the drawRect method could make those things not work correctly.
Buttons normally draw a title and a rounded rectangle frame. You can turn that off by setting the button's type to custom.
Custom buttons will draw an image if you install one.
Buttons normally also either draw a highlight over their image, or have a second image to use for the highlighted state. If you want to implement drawRect, you'll need to handle drawing the highlighted state yourself.
In general, you want to avoid using drawRect and use some other technique to get the content you want into your views.
What, exactly, are you trying to do?
I have subclassed UINavigationBar and overridden drawRect method so I can add a gradient to it. Now, I want to use the same gradient to the back button, but I don't know what is the easiest way (subclass UIButton, subclass UIBarButton item)
Thank you.
A lot of people on StackOverflow caution against subclassing UIButton.
create uibutton subclass
The easiest way to do what you want is to use [UIButton buttonWithType:UIButtonTypeCustom].
Make a transparent .png image of your button and set it as your button.image.
You can have a look here:
http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/
explore: http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/
Basically I want to have a custom UIView that has a bunch of buttons in it. Should I just add the buttons to the view inside initWithFrame? I also would like to set up all their locations in the view at the same time. Is there a customary place where all this would be done? I'm guessing I shouldn't do anything in drawRect since I'm not making any custom drawings, just adding buttons as subviews. Thanks.
initWithFrame: is a good place to add the buttons.
If the buttons' frame depends on the parent view's size, you should set the frame of the buttons in the parent view's layoutSubviews method. Otherwise you can just specify the buttons' autoresizingMask when you add the buttons.
You can't set up all their locations "at the same time", but if you call many addSubview: sequentially you don't see even that were added one at a time.
You do not always need to subclass UIView(if it is not particularly complex), you can create it at run time, add the components that you want with initWithFrame: and show it; otherwise you can subclass UIView just to create the layout (rounded border, gradient color, etc), initialize it from your class and add the components. If you have some problem, show us more details about your problem.
I have an image that I want to use as the background of my UIViewController. I can add it as a UIView. The point is that I want to add some transparency to it. I don't want the image to be as opaque as the original image, because it has some graphics on it and would be way too distracting. So I want to make it faded, sort of. I hope that makes sense.
Maybe I should subclass UIView? Can anyone show me the code to do something like this?
you can set alpha of the uiview from the xib else you can use [self.view setAlpha:0.5]
Does anybody know how to disable gradient in UINavigationBar and make it totally black?
Create a class that descends from UINavigationBar and implements its own drawRect: method. You may have to draw the title and buttons yourself (in which case, why not just use a custom view?)