iPhone SDK - UITabBarConroller and custom design - iphone

I am having a problem with my tab bars at the bottom of the screen. The designer has decided it should be one colour (not black) when inactive and another colour when active. I have worked out how to replace the main colour of the tabbar by subclassing UITabBarController and doing this:-
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
UIView *v = [[UIView alloc] initWithFrame:frame];
//get percentage values from digitalcolour meter and enter as decimals
v.backgroundColor = [UIColor colorWithRed:.0 green:.706 blue:.863 alpha:1];
[tabBar1 insertSubview:v atIndex:0];
[v release];
}
I just can't see how to make the active tabbar be a separate colour when it is selected. I have tried subclassing UITabBarItem but there doesn't seem to be any property for me to set to change the background colour of the tab.
They also want to have the icons on the tab bar not be blue and grey and I can't figure out how to do that. In the ViewController for one tab bar item I have put this into viewdidload:-
myTabBarItem *tabItem = [[myTabBarItem alloc] initWithTitle:#"listOOO" image:[UIImage imageNamed:#"starcopy.png"] tag:1];
tabItem.customHighlightedImage=[UIImage imageNamed:#"starcopy.png"];
self.tabBarItem=tabItem;
[tabItem release];
tabItem=nil;
and in my subclass of UITabBarItem I have put this:-
-(UIImage *) selectedImage{
return self.customHighlightedImage;
}
Only I don't see the icon at all.
If I put this into the viewDidLoad of my subclass of UITabBarController:-
for (UITabBarItem *item in tabBar1.items){
item.image = [UIImage imageNamed:#"starcopy.png"];
}
Then all my tab bars have the icon but they are blue (and grey when inactive) how would I get them not to become blue but stay their original colour?
If you have any light on this problem please help as I have been banking my head on the wall for 2 days now and it's getting me down.
Thanks in advance
Cheryl

It seems to be impossible to be done without using private API calls. The only solution I can see right now is to create a complete custom TabBar. That however is a lot of work just to get an icon presented in a different color.

Related

Adding Color to UITabBar - Very very Weird Bug?

So,
I am subclassing my UITabBarController and I add the color Green to my UITabBar.
- (void)viewDidLoad {
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
v.backgroundColor = [UIColor greenColor];
[v setAlpha:0.35];
[[self tabBar] insertSubview:v atIndex:0];
[v release];
[super viewDidLoad];
}
This does not work .
But if I try [[self tabBar] insertSubview:v atIndex:1]; it Works .. and a green color is added to the entire TabBar !!
I work on the iPhone Simulator using ios5 SDK and it works properly and I am able to select all tabs.
But when I try this code on the ios4.2 (real iPhone), the first tab is visible.. but DOES NOT RESPOND TO TOUCH
The reasons is clear, its because of adding a SubView of greenColor # index ==1;
But If I change to [[self tabBar] insertSubview:v atIndex:0]; my firstTab is selectable, but the tabBar just does not turn Green !!
Kindly Advise !!
Thanks.
Edit:
So the problem is associated with the iOS5 and the earlier versions. I will edit the answer and put back the original answer once iOS 5 is released.
Right now.... Off to the developer forums !!! :)
You might want to create a custom control for this that does not subclass UITabBarController. From the documentation, "You should never access the tab bar view of a tab bar controller directly."
The view layers and the hierarchy part of the non-public API and subject to change across different version of the iOS.

Using background images for UITableViewCells (grouped style) - corners not rounded

I'm trying to create a grouped style tableview which features cells with a background image. Both the tableview and the cells have been set up in Interface Builder.
Everything works, however the cells won't clip the background images corners, leaving the cell square. I've tried to enable "clip subviews", I've tried adding an UIImageView as a subview (as opposed to making the background view an imageview), I've both tried selecting a background image directly or connecting a seperate UIImageView to the cell's backroundView Outlet - to no avail.
I've tried setting up the cell programmatically, too - doesn't work. It seems the only thing that will leave the corners rounded is selecting a background color (not an image) directly in Interface Builder, which is not what I want.
There are other questions on SO with related problems, none of which covered the use of background images however, so no help there.
Thanks alot for any insights..!
Have you tried _table.backgroundColor = [UIColor clearColor]; ?
FIXED: _cell.backgroundColor = [UIColor colorWithPatternImage:image];
The above did not work for me.
first set the background of your window to the color you want in your app delegate like this:
UIView *bgView = [[UIView alloc] initWithFrame:self.window.frame];
bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"main_background.png"]];
[self.window addSubview:bgView];
[bgView release];
Now if you just have one view controller just set its background to clear and your done. I had to to the next steps because I was popping a modal window with a table view and if the background of that view was clear it looked choppy coming up. So the code below fixed the choppy view and I still get the rounded corners because after the view has appeared I set the background to clear again and let the background from the window show through.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"main_background.png"]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.view.backgroundColor = [UIColor clearColor];
[self.tableView reloadData];
}

How do I show a UISearchBar on the right side of a UINavigationItem?

I am trying to display a UISearchBar in place of the button shown on the right side of the UINavigationItem. I am using this code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] init];
UIBarButtonItem *navRight = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[[self navigationItem] setRightBarButtonItem:navRight];
[searchBar release];
[navRight release];
}
However, the displayed search bar is only a few pixels wide. I need to make it wider.
Am I going about this the wrong way? Is it at all possible?
You need to set the UISearchBar's frame at some point.
e.x.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
EDIT: Regarding the button-esque artifact under the UISearchBar
The button-y thing that you see under the UISearchBar is actually a background element of the UISearchBar, not the result of the search bar overlapping a UIBarButtonItem. Since there doesn't seem to be a nice way to hide this (and I hope someone can come along and correct me), I can only point you in the direction of a hack that comes with the usual perils of SDK version dependence, inelegance, and potential for more headaches.
EDIT 2: Another potential solution
You could also set your UISearchBar's frame's height to 44 (the height of the navbar) and then the background should blend nicely with the navbar. Based off this SO post I recently saw.

UISegmentedControl in Mail app

How do I get a UISegmentedControl that is like the one in the Mail App, so that it is the same colour as UIToolbar buttons (as if both segments were in the selected state).
I want to use the segmented control for exactly the same purpose as Mail.
(on the iPad, so a grey not blue color)
This is code from Apple Sample codes... NavBar and both the images used in the code..
you shoud be able to get exact same view as mail App.
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
You seek the tintColor property!
When you use a UISegmentedControl you can change its tint color to any color you can dream up. So, if you added the UISegmentedControl in Interface Builder then you would style it in your - (void)viewWillAppear:(BOOL)animated method as such (assuming you had it hooked up to a #synthesized ivar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set the tintColor to match the navigation bar
self.mySegmentedControl.tintColor = [UIColor colorWithRed:.94 green:.94 blue:.94 alpha:1];
... do whatever else in your viewWillAppear ...
}
Now obviously you will want to play with the red, green, blue, and alpha's that I've put in the sample code above, but you can literally tint the UISegmentedController any color you would like (or make it as transparent as you would like), so it's just a matter of finding the RGBA values that look perfect to you.
Remember that per Apple's docs that the default value of this property is nil (no color). UISegmentedControl uses this property only if the style of the segmented control is UISegmentedControlStyleBar.
Good luck!
I dont know exactly what you mean.. but i believe the "UISegmentedControlStyleBar" as segmentedControlStyle could it be.
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar
You can set this property in the IB too! (It's the property called "style")
The style I'm looking for is undocumented: it is style 4.
It looks like he up/down control here: http://media.mobilemeandering.com/wp-content/uploads/2010/04/ipad-mail-message-2.png
(not my image btw)
It basically makes all segments look selected, it's intended for momentary pushes, and is effectively multiple tool bar buttons pushed up together.
So it can't be set in IB but must be set in code or manually in the nib/xib file, by opening the nib as a text file.
I'm not sure I exactly understand what you're trying to do, but I'll give it a shot.
The solution is not obvious, you need to use a UISearchDisplayController in order to get a matching UISearchBar and UISegmentedControl.
See the TableSearch sample code for an example.

Why are there left/right gaps when I add an image to my navigationBar?

UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.
That's... probably not how you want to do that—it's not what the titleView is for. The usual way to do a custom navigation-bar background is to create a category on UINavigationBar, like so:
#implementation UINavigationBar(MyCustomBackground)
- (void)drawRect:(CGRect)r
{
[[UIImage imageNamed:#"my-navigation-background.png"] drawInRect:self.bounds];
}
#end
Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.