change uibarbuttonitem background using image - image

i have an uibarbuttonitem, but i was initialize it using initwithcustomview. I want to change it backgrounds using an image, but i dont know how to do. I was using setBackground method, like this
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
but the uibarbuttonitem still not show the image, it's just change the segmented control background, not the barbutton.
Can somebody help me?

Perhaps you want to change the tint-color (#property(nonatomic, retain) UIColor *tintColor) because UISegmentedControl has no background-color (just because it inherits it from UIView does not mean it uses it though)

Related

xcode: segmented control appears on sim, not on device

I have an iPad app with several segmented controls, including one view with two controls.
On that view, everything shows in the sim, both normal and retina. However, in the device, only one shows.
Below is the code that does not show on the device.. I have checked and all the images that make up the control are copied in the bundle resources. I've tried removing, cleaning, etc. No joy. I must be missing something (hopefully simple)..
UISegmentedControl *controls = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
nil]];
CGRect frame = CGRectMake(35, 70, 700, 35);
controls.frame = frame;
}
[controls addTarget:self action:#selector(drawSegmentAction:) forControlEvents:UIControlEventValueChanged];
controls.segmentedControlStyle = UISegmentedControlStyleBar;
controls.momentary = YES;
controls.tintColor = [UIColor grayColor];
[self.view addSubview:controls];
}
For reference, this code in the same view does work:
For reference, this control code does work:
-(void) buildColorBar {
//NSLog(#"%s", __FUNCTION__);
UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects: [UIImage imageNamed:#"White.png"],
[UIImage imageNamed:#"Red.png"],
[UIImage imageNamed:#"Yellow.png"],
[UIImage imageNamed:#"Green.png"],
[UIImage imageNamed:#"Blue.png"],
[UIImage imageNamed:#"Purple.png"],
[UIImage imageNamed:#"Black.png"],
nil]];
NSLog(#"Portrait");
CGRect frame = CGRectMake(35, 950, 700, 35);
colorControl.frame = frame;
// When the user chooses a color, the method changeBrushColor: is called.
[colorControl addTarget:self action:#selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged];
colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Make sure the color of the color complements the black background
colorControl.tintColor = [UIColor grayColor];
// Add the control to the window
[self.view addSubview:colorControl];
}
Is there a rule against using two segmented controls in one view?
I found that one image in the segmented control - while it looked like it was in the bundle - it wasn't. PITA, but at least, it works..

How to stretch (scale) image on a UIView?

How can i take an image and stretch it to fill the screen on my UIView (320/460)
I am loading and placing it, however as you can see, some scaling is needed.
Thank you!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
Like I said in my comments above to Shamsiddin's post, the UIImageView needs a size to know where to stretch to. Here I'm using the same size of the UIView. I've added another answer because the code to UIView is missing. So here's the complete code.
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
try
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
I just did this in my own iOS5 app, i needed to dynamically set the background image and scale it. Imagine you are doing this in - (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
So we create the view, set it's scaling mode, add the bg image, set it as the subview in the main view and then push it to the back so it does not occupy any other subviews.
Please accept this answer.

UIBarBUttonItem image

I have a toolbar created programaticaly with 3 buttons (only 1 shown here). After a lot of Googling I was able to set a nice background image but now I don't know where to put
the code for the action: target:self action:#selector(pressButton3:) method. So I have a non-working button with nice colorful image. I've tried a lot of examples and if the button is working the image is not working and vice versa. Please help.
//Add buttons
UIImage *buttonImage = [UIImage imageNamed:#"mapp.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button];
[systemItem1 initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:#selector(pressButton1:)];
//add to array
NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];
I think you're button should work if you add:
[button addTarget:self action:#selector(pressButton1:) forControlEvents:UIControlEventTouchUpInside];
And remove the line:
[systemItem1 initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:#selector(pressButton1:)];

iphone: customise cell.accessoryType

hi am doing an iphone twitter app with json and i am wondering if it is possible to set the
cell.accessoryType to a image in UITableView? or possible move the image to the right hand side...any clues?
Sure. Just use the accessoryView property of UITableViewCell and assign a UIImageView:
UIImage *image = [UIImage imageNamed:#"fileNameOfYourImage.jpg"]; //or wherever you take your image from
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
cell.accessoryView = imageView;
[imageView release];
No need to use imageview, just provide this for simple arrow.
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

UIBarButtonType as a UIButton. Not on a NavigationBar!

I would like to use the better styled UIBarButtonType as a typical UIButton. This is just to use on a view not a navigationBar.
I don't really want to recreate the button in an image app and apply to the various states. I know how to do it the other way around so there must be a way.
Please help.
I tried this and it works :
UIButton *info = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBis = [[UIBarButtonItem alloc] initWithCustomView:info];
NSArray *array = [[NSArray alloc] initWithObjects:infoBis, nil];
[tool setItems:array animated:YES]; //tool = UIToolbar
don't forget to put a target on your UIBarButtonItem ;)
You cannot do that.
UIBarButtonItems are made just for UIToolbar and UINavigationBar.
You need to create your own background image. It isn't a big issue, just few lines.

Resources