UIKit Appearance Proxy for Custom UITabBar - iphone

I want to create a custom UITabBar and am familiar with the appearance proxy. I know I can set its background image to whatever I want. How can I change the selected state of each tab? Essentially I would like to remove the gloss/shine effect.

You need to grab the actual UITabBarItem
Something like this should do the trick for individual items
UITabBar *tabBar = tabBarViewController.tabBar;
for(UITabBarItem *tabItem in tabBar.items)
{
//in reality you will probably change these images and grab from the array individually
UIImage *selectedImage = [UIImage imageNamed:#"selected.png"];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected.png"];
[tabItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
}
alternatively you can just use the proxy to alter all UITabBarItems using
[UITabBarItem appearance]

You may use the appearance proxies for UIBarItem and UITabBarItem to accomplish this.
From Apple's documentation:
Customizing Appearance
In iOS v5.0 and later, you can customize the
appearance of tab bars by setting item label text attributes using
appearance selectors declared by UIBarItem. You can also use the
methods listed in “Customizing Appearance.” You can customize the
appearance of all segmented controls using the appearance proxy (for
example, [UITabBarItem appearance]), or just of a single tab bar. You
can also provide finished selected and unselected images using the
methods listed in “Managing the Finished Selected Image”; these
methods, though, so not participate in the UIAppearance proxy API (see
UIAppearance). UIKit does now provide any automatic treatment to
finished images. For good results, you must provide finished selected
and unselected images in matching pairs using
setFinishedSelectedImage:withFinishedUnselectedImage:.

Related

Set UIToolbarPosition for created UIToolbar

I'm writing app which is aimed only for iOS5 devices, so I'm trying to maximize usage of new appearance API.
I can change background of my UIToolbar with following method:
[[UIToolbar appearance] setBackgroundImage:<myImage> forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Everything works fine, but I'm looking for more customization, so I was wondering about usage of UIToolbarPosition and I run into some problems. Using InterfaceBuilder or adding UIToolbar programmatically I'm positioning it at the top of main view - so I'm expecting UIToolbarPosition to be set as UIToolbarPositionTop, but from what I'm testing it doesn't work automaticly nor can I find any API which allows me to set UIToolbarPosition (Yes, I googled it already).
So - main question - how to create UIToolbar and tag it properly, so it can response only to UIToolbarPositionTop or UIToolbarPositionBottom, so I can use:
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:<myImage_2> forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
EDIT: More details, as asked.
I have multiply toolbars in my application - for instance, toolbars at top of the screen on 2 screens, toolbars acting as accessory views for keyboard and 2 toolbars at the bottom of modals screens. I'd like to maximize usage of new appearance API instead of customizing toolbars on each screens, hence I'm asking about whole UIToolbarPosition thing and how to use it.
I know I can achieve what I want just by customizing each UIToolbar separately, but I'm just curious about new API and UIToolbarPosition usage.
UIToolbarPostion isn't a property that you're supposed to be setting programmatically - instead, it allows you to tell the program how to handle a toolbar when it's in different positions. This is mostly for toolbars that are getting pushed around by screen changes (autorotation) or are on something like a navigation controller that has variable content.
That being said, if you want to directly access your toolbars so that you can use them/set properties/etc there are a couple of methods. It sounds like you know what tagging is, and this is a valid method - just give the toolbar a tag in IB or programmatically (either edit the tag property in the side bar for IB or use the .tag property when you declare the toolbar). Then you can use the viewWithTag method to access your toolbar. However, a better method would be to just create an IB property for the toolbar (same as with labels or buttons) by control-dragging over to the header file from the toolbar. Then you could just write [nameOfToolbarProperty doSomeMethod]. If you're creating your toolbar with code then just make a reference to it the same way e.g.
UIToolbar *tref = [/*toolbar creation code*/];
In conclusion, your code
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
could be made to work by adding
//Connect this to your toolbar in Interface Builder
#property (weak, nonatomic) IBOutlet UIToolbar *tref;
to your header. Then just do
[tref setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
Note that this just tells the program what image to display if the toolbar is in the top position - it does not set the position of the toolbar. UIToolbarPosition is a constant (so you cannot set it).

UISwitch customization?

Is possible to add images to an UISwitch background,like when the state is ON (be one background) and when the state is OFF (another background image)?
To change the background color (not image) all you have to do is below. This changes the off color for all UISwitch controls in realm.
[[UISwitch appearance] setTintColor:[UIColor brownColor]];//Off Color
_locationSwitch.onTintColor = [UIColor orangeColor];//On Color
If you want to use image, use the following cool hack of using the segmented control.
https://stackoverflow.com/a/5088099/1705353
If you want to write a lot of code, you can write your own control. Valeriy Van pointed to: https://github.com/homick/iPhone-Snippets/tree/master/General
Also another resource is: http://www.raywenderlich.com/23424/photoshop-for-developers-creating-a-custom-uiswitch with code at: http://cdn2.raywenderlich.com/wp-content/uploads/2012/10/CustomSwitchResources.zip
I would like to suggest you to follow Custom UISwitch & App Store approval to have a better idea, which is suggesting you to create a custom switch control.
You can recreate a custom UISwitch by subclassing UIControl. By doing this you can have full control over what the switch looks like. You can look take a look at SevenSwitch. A custom UISwitch replacement I've created. The on/off colors can be customized to your liking.
https://github.com/bvogelzang/SevenSwitch

How to change UITableView's appearance?

What different kinds of things can you do to edit a tableview's appearance and how would you do them? For example, how would you change the color programmatically? Or change the navigation bar's color programmatically?
Check out the docs for UITableView and UITableViewCell.
You probaby want to change the backgroundColor property or customize each cells contentView property. Also,look into layers, you can do things like make rounded corners, shadows, etc on a views layer.
Here is the QuartzCore Framework docs. They should be useful if your trying to change the appearance of a view.
Edit (good suggestion bshirley):
AboutTableViewsiPhone
Check out Apple's doc on UITableView particularly the configuring A Table View section.
[tableView setBackgroundColor:[UIColor grayColor]];
or You can use,
[tableView setBackgroundColor:[colorWithRed:.98 green:.98 blue:.82 alpha:1]];
If you want to change the appearance of all TableViews in your app you can define it with the Appearance property. In Monotouch you do something like this:
UITableView.Appearance.BackgroundColor = UIColor.Black;
UITableViewCell.Appearance.BackgroundColor = UIColor.Black;
In objective C it would be something similar.

Add image to lower bar

How to add image to lower bar in tab based application
I can get the
self.navigationitem
but how to access the lower toolbar anda add background image to it
best regards
What you're looking for is a UITabBarITem that you can obtain from the items property of a UITabBar, or more easily from the tabBarItem property of your UIViewController.
Then I guess you can set your image using image property...
In conclusion :
self.tabBarItem.image = [UIImage imageNamed:#"myBeautifulImage.png"];
might be what you're looking for (self being, ofc, your UIViewController)...
EDIT: but if your "lower bar in tab based application" is a UIToolbar (/me :grins:..) then what you are looking for is a standard UIBarButtonItem that can be obtained from UIToolbar.items property OR some IBOutlet property if you wired it using Interface Builder.
In any case, I guess looking at Apple sample codes might be a good idea.

Possible to create UITabBarController w/ Custom Images, Pressed/NotPressed?

I want to add a little flair to my application and while I have been somewhat successful, I haven't figured out a way to truly do what I want to do. I would try to subclass UITabViewController, but I don't think I would even know where to start or if that is the right approach.
Basically, what I want to do is have custom images for each tab bar button. I have both pressed and non-pressed images. Instead of using the highlight that is generated over the current icon, I want the pressed image to be shown. What I've been able to do is create a Category of the UITabBar with a custom drawRect method that basically draws all of my custom tab bar icons. I then initialize the same amount of UITabBarItems w/out specifying an image or title of any kind which I then add to the array of items. This lets me have my icons and then just overlays the highlight on them.
This is ok I guess, but I would really like to have the pressed button look.
Here is the code I use for drawing the tab bar:
#implementation UITabBar (CustomImage)
-(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"TabBar.png"];
[image drawInRect:CGRectMake(0,0,320,50)];
}
#end
And then I initialize the tab bar items like this:
UITabBarItem *homeTabBarItem = [[UITabBarItem alloc] init];
If anyone knows how I would do this, that would be greatly appreciated. Please keep in mind that I'm still relatively new to Objective-C, so I'm somewhat confused as to how I would correctly subclass anything at this point.
The functionality I'm looking for would be similar to what RougeSheep was able to accomplish with Postage: http://postage.roguesheep.com/ They did an awesome job, and this is kind of what I'd like to emulate.