UISegmentedControl with buttons - iphone

So I have a UISegmentedControl with custom backgroundImage.. Here's how I set it up:
UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 178, 30)];
[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentedCtrl setTintColor:[UIColor clearColor]];
[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBezeled];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"btn-home-down.png"] atIndex:0 animated:NO];
.....
[segmentedControl setImage:[UIImage imageNamed:#"btn-home.png"] forSegmentAtIndex:0];
[segmentedControl setImage:[UIImage imageNamed:#"btn-popular.png"] forSegmentAtIndex:1];
[segmentedControl setImage:[UIImage imageNamed:#"btn-my-photos.png"] forSegmentAtIndex:2];
[segmentedControl setImage:[UIImage imageNamed:#"btn-places.png"] forSegmentAtIndex:3];
[segmentedControl setImage:[UIImage imageNamed:#"btn-my-likes"] forSegmentAtIndex:4];
Now the issue is this:
If you see closely between the segment image, on the divider line at the bottom I sort of have a white dot. All of my images are set to square.. why is this and how can I eliminate this?

Maybe you could get a UIImageView with only one pixel colored in with the color that the white pixel should be. Then you could placed it on top of the white pixel. This probably isn't the best way of doing this, but it works.
Hope this helps.

Related

customize UIBarButtonItem initWithBarButtonSystemItem to show up as grey not white in iOS 5

I am using a white background on my UINavigationBar with darkGrayColor text
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];
Which looks nice but I have buttons defined like this throughout my app:
self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchProducts)];
I killed the default blakc background look I had going like this:
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"uibarbuttonbackground"] forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Which is just a white square.
Now my icons are still white with a light gray shadow, anyway to change these default icons?
Once I tried to change background of UIBarButtonSystemItemCamera but nothing worked. I guess these default system button types (UIBarButtonSystemItem )come with a background image already, so changing it won't do.
I recommend you to create a custom view and init your bar button with it. Not a perfect code but I hope it helps.
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(takePicture:) forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
I used the category solution found here, it is working great:
UIBarButtonItem with custom image and no border
I little more work than I was hoping but it is a nice clean solution.

setting background image of UISegmentedControl

So I have the following code to set the segmented control background color:
UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-home.png"] atIndex:0 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-star.png"] atIndex:1 animated:NO];
It all works fine except that when I click on the segment I can see the blue button highlighted. How do I disable this highlight? Here's a screenshot of my problem:
UISegmentControl has a Divider between two segments. By using the following code you can change the divider background.
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_selected.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_normal.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Where divider_selected.png is a image which you used for selected state. Where as divider_normal.png is a image which you used for normal state.
Hope this will help....
Set your same background images for the highlighted state:
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

adding buttons to a bottom toolbar

I have the following code:
self.shareButton=[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 35, 35)];
[shareButton setBackgroundColor:[UIColor blueColor]];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button"] forState:UIControlStateNormal];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button_pressed"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController setToolbarItems:[NSArray arrayWithObject:shareButton] animated:YES];
Why I cannot see toolbar buttons? What's the problem?
What meggar says is probably the right answer. Try doing it this way.
--UPDATE--
Since the problem still exists I have updated the example to move the set call to the view controller instead of the navigation controller.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
[self setToolbarItems:[NSArray arrayWithObject:barButtonItem] animated:YES];
[barButtonItem release];

iphone UIbutton with image click problem

I have 3 UIbuttons in my with background image in my app...
for all those 3 buttons if images are assigned only lower half portion of button is clickable and upper half doesn't work at all..
any idea why ?
I tried to put color for buttons to check if they have right CGRect but it looks proper...
Here is some code
UIButton* segment=[[UIButton alloc] initWithFrame:CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34)];
segment.backgroundColor = [UIColor redColor];
[segment addTarget:self action:#selector(segmentActionButton:) forControlEvents:UIControlEventTouchUpInside];
[segment setTag:i];
segment.titleLabel.font=[UIFont fontWithName:#"Arial" size:12.0];
segment.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[segment setTitle:[mItems objectAtIndex:i] forState:UIControlStateNormal];
[segment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_INACTIVE_MAP] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_ACTIVE_MAP] forState:UIControlStateHighlighted];
[self addSubview:segment];
[mButtonArray addObject:segment];
[segment release];
Hope it helps..
May be the error occurs because you are creating button in alloc+initWithFrame way. I'm not sure but try to create button in such way:
UIButton* segment = [UIButton buttonWithType:UIButtonTypeCustom];
segment.frame = CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34);
And remove line [segment release]; as you will create autoreleased object.

add image to UIBarButtonItem for UIToolbar

I am trying to add an image to the UIBarButtonItem which I have to use in a UIToolbar.
I have managed to read the image and even get it to display with a UIImageView, but when i add it to the UIBarButtonItem and then add that item to the UIToolbar, the toolbar just displaces a "Blank White" space the size and shape of the image that I am trying to load.
here is what I am trying.
UIImage *image = [UIImage imageNamed:#"6.png"];
//This is the UIImageView that I was using to display the image so that i know that it is being read from the path specified.
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 50, image.size.width, image.size.height);
[self.view addSubview:imageView];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image forState:UIControlStateNormal];
//This is the first way that I was trying to accomplish the task but i just get a blank white space
//This is the Second way but with the same blank white result.
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2];
NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];
//Adding array of buttons to toolbar
[toolBar setItems:items animated:NO];
//Adding the Toolbar to the view.
[self.view addSubview:toolBar];
Your help will be much appreciated.
Thank You!
Shumais Ul Haq
Other than you'd normally expect in UIKit stuff, you might need to set a frame for the button explicitly. Maybe that's your problem.
This is what I wrote for a custom styled back button, as a category to UIBarButtonItem (but you can just take the pieces you need from it).
Note that this was used for the navigation bar, not the toolbar, but I presume the mechanics are the same, since it is a UIBarButtonItem as well. For UIToolbar you can just use IB to get it right at compile time.
#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f
+(UIBarButtonItem*)arrowLeftWithText:(NSString*)txt target:(id)target action:(SEL)selector
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [[UIImage imageNamed:#"arrow_left.png"]
stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchDown];
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[btn setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
[btn.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:FONT_SIZE]];
[btn.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];
/**** this is the magic line ****/
btn.frame = CGRectMake(0.0f,0.0f,
[txt sizeWithFont:[btn.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN,
IMAGE_HEIGHT);
[btn styleBarButtonForState:UIControlStateNormal withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateDisabled withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateHighlighted withImage:img andText:txt];
[btn styleBarButtonForState:UIControlStateSelected withImage:img andText:txt];
return [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
}
usage:
[UIBarButtonItem arrowLeftWithText:#"Back" target:self action:#selector(dismiss)];