I have the following code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
webControlsToolbar_ = [[UIToolbar alloc] init];
[self.webControlsToolbar_ setTintColor:[UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1]];
Any idea why the color is still solid black? In my view did load I added the toolbar:
self.webControlsToolbar_.frame = CGRectMake(0, self.view.frameHeight - self.webControlsToolbar_.frameHeight + 1.0, self.view.frameWidth, self.webControlsToolbar_.frameHeight);
[self.view addSubview:self.webControlsToolbar_];
I've run into a similar problem before. I resolved it by setting the tint color immediately before showing the toolbar in code. See if that works for you.
I find it easier to set the background color of a button, label, etc. exactly as I want with the utility bar, then create an outlet called say, myOutlet, and do this:
[self.webControlsToolbar_ setTintColor:myOutlet.backgroundColor];
The issue is at the line
self.webControlsToolbar_.frame = CGRectMake(0, self.view.frameHeight - self.webControlsToolbar_.frameHeight + 1.0, self.view.frameWidth, self.webControlsToolbar_.frameHeight);
Try to NSLog a self.webControlsToolbar_.frameHeight property and you should see that is zero after [UIToolBar new]; Use a constant height instead.
You have to change this attribute in the viewDidLoad method:
-(void)viewDidLoad{
[super viewDidLoad];
//Custom initialization
[self.webControlsToolbar_ setTintColor:[UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:246.0/255.0 alpha:1]];
}
Related
How come the CheckBox does not load any of the images if I do this? It only works if I set them from the XiB files. I can do this from Xib, but if I do need more complex things to customize, I won't know how.
I set a break point there so I know these things are being called.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[tcCheckBox setBackgroundImage:[UIImage imageNamed:#"checkbox-unchecked.png"] forState:UIControlStateNormal];
[tcCheckBox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[tcCheckBox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"]
forState:UIControlStateHighlighted];
tcCheckBox.adjustsImageWhenHighlighted=YES;
**// Custom initialization**
}
return self;
}
This is because initWithNibName:bundle: doesn't instantiate the nib right away but just initializes the view controller, so all your IBOutlets are still nil at this point. Override viewDidLoad for custom UI initialization, this method gets called when the nib gets instantiated and all your IB connections are resolved.
I want to display a custom bar that appears on every screen of my application with buttons that work. I add the CustomViewController to my classes in the init method, and everything works correctly, except when I analyze my application I get a potential memory leak.
When I release [customViewController release], the buttons on the CustomViewController will no longer work. What is the proper way to go about implementing this solution with no memory leaks.
#import "CustomViewController.h"
#implementation CustomViewController
- (IBAction)doSomething:(id)sender
{
// Perform an action
}
#end
A ViewController which I create the CustomViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
CustomViewController *customViewController = [[CustomViewController alloc] initWithNibName:#"CustomViewController" bundle:nil];
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[bar addSubview:customViewController.view];
[self.view addSubview:bar];
[bar release];
}
}
You seem to be going about implementing this the wrong way. What you actually need to do is create CustomViewController and add your toolbar to that view. Every other view controller in your app should then be made a subclass of CustomViewController.
If a custom navigation bar is the only thing you're using this superclass for, I would recommend just styling the bar directly on the UINavigationController your app is using.
The proper solution was to create a container view, and place my custom task bar inside of that view.
You said your bar has buttons? Does it release those buttons when it itself is released? Check it's viewDidLoad function.
I am using Three20 to create a thumbnail view. I want to change the navigation bar style to black from black translucent. If I give blacktranslucent it works fine, if I change it the thumb nail view is lowered like this image.
How can I change it?
You can override the init method, and change whatever you want there. For instance :
// MyThumbsController inherits from TTThumbsViewController
#implementation MyThumbsController
...
- (void) setCustomNavigationBar
{
// Navigation bar logo
UIImage *barLogo = [UIImage imageNamed:#"bar-logo.png"];
UIImageView *barLogoView = [[UIImageView alloc] initWithImage:barLogo];
self.navigationItem.titleView = barLogoView;
// Navigation bar color
self.navigationBarTintColor = HEXCOLOR(0xb22929);
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Customization
self.hidesBottomBarWhenPushed = NO;
[self setCustomNavigationBar];
}
return self;
}
I used this to hide the bottom bar
-(BOOL) hidesBottomBarWhenPushed{
return YES;
}
Now i'm at a point where I want to NOT to hide it anymore. What method should I use?
Thanks
Take at look at Elements sample project. They do something like you want, especially in the ElementViewController.m file.
Thats very simple, just use this:
[tabBar setHidesBottomBarWhenPushed:FALSE];
It took me some time putting the puzzle of John together. So here is my final result. In the .m file of my view controller I add this code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.hidesBottomBarWhenPushed = YES;
}
return self;}
Because I use a nib file I had to override the initWithNibName method.
A really simple way:
Class *instanceName = [[Class alloc] init];
instanceName.hidesBottomBarWhenPushed = YES;
...
[navigationController pushViewController:instanceName animated:YES];
How can I change the color of navigation bar from its default blue color?
Thanks.
The UINavigationBar class has a UIColor *tintColor property that you can change in code.
Alternately this property is also exposed in the InterfaceBuilder UI design tool.
Assuming you have added the navigation bar programmatically and not in Interface Builder, just put this in your viewDidLoad method.
self.navigationController.navigationBar.tintColor = [UIColor grayColor];
TintColor property doesn't affect default subview of navigation bar as bottom border and shadow. Sometimes it's useful to override layout of navigation bar at all.
Despite navigationBar is read-only property for UINavigationController you can avoid
this restriction by "setValue:forKey:". This method was approved on 5 applications successfully submitted to AppStore.
You can subclass UINavigationBar and change drawRect: method as you want.
For example,
#implementation CustomNavigationBar
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = ImageFromColor(WANTED_COLOR);
[backgroundImage drawInRect:rect];
}
After you can subclass UINavigationController and change initWithRootViewController:
- (id) initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self)
{
CustomNavigationBar *navBar = [CustomNavigationBar new];
[self setValue:navBar forKey:#"navigationBar"];
}
return self;
}
Also you can vary this approach by making Category for UINavigationController and implementing method swizzling for initWithRootViewController:
P.S. Yesterday my new app appeared at AppStore without any problem with this approach.