My custom UISegmented control has background images [for each state]. How do I add text above the images [for each state, meaning that when index 0 will be selected I will have a specific color and font] ?
Here's how my UISegmented control looks like:
UISegmentedControl *customUISC=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:#"ON",#"OFF",nil] autorelease]];
[customUISC setFrame:CGRectMake(11,14,298,28)];
customUISC.selectedSegmentIndex=0;
customUISC.segmentedControlStyle=UISegmentedControlStyleBar;
[customUISC setImage:[UIImage imageNamed:#"FirstTabActive.png"] forSegmentAtIndex:0];
[customUISC setImage:[UIImage imageNamed:#"SecondTab.png"] forSegmentAtIndex:1];
[customUISC addTarget:self action:#selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];
and the checkOnOffState method looks like:
-(IBAction)checkOnOffState:(id)sender{
UISegmentedControl *iSeg=(UISegmentedControl *)sender;
if(iSeg.selectedSegmentIndex==0){
[iSeg setImage:[UIImage imageNamed:#"FirstTabActive.png"] forSegmentAtIndex:0];
[iSeg setImage:[UIImage imageNamed:#"SecondTab.png"] forSegmentAtIndex:1];
}
else {
[iSeg setImage:[UIImage imageNamed:#"FirstTab.png"] forSegmentAtIndex:0];
[iSeg setImage:[UIImage imageNamed:#"SecondTabActive.png"] forSegmentAtIndex:1];
}
}
I'm not sure I fully understand what you're after, but could you just make use of UILabels (positioned above the images). You could then change the colours and text quite easily using myLabel.text and myLabel.textColor.
Hope this helps in some way!
Related
I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties here...
UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(165, 205, 65, 40)];
[myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal];
[myButton setTitle:#"Hi....." forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
[cell addSubview:myButton];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
I have following code and when I call this method, it doesn't change the image of the button although there are images in the resources.
-(void)changeButton:(NSString*)button withURL:(NSString*)url {
timetableURL = url;
UIImage *btnImage;
if ([button isEqual:#"Browser"]) {
btnImage = [UIImage imageNamed:#"browser-button.png"];
[safariBtn addTarget:self action:#selector(onSafariButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([button isEqual:#"Timetable"]) {
btnImage = [UIImage imageNamed:#"browser-timetable.png"];
[safariBtn addTarget:self action:#selector(onTimetableButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
[safariBtn setImage:btnImage forState:UIControlStateNormal];
[btnImage release];
}
UPDATED
FYI: It's a button upside UIToolbar.
Some things that seem to be wrong with your code:
[btnImage release]
You've created that image using autorelease, you are not responsible for its release. This will most likely end up in weird stuff happening, i.e. what you encountered.
Are you sure that btnImage has contents after that if statement? is there a possibility that button is not equal to #"Browser" nor #"Timetable"?
Also, make sure the button is in its normal state.
Cheers
Try This ,
[safariBtn setBackgroundImage:btnImage forState:UIControlStateNormal];
No need to release btnImage ...
i think this should help you...
[safariBtn setImage:[UIImage imageNamed:#"browser-button.png"] forState:UIControlStateNormal];
may be this will help you : set the titles of button where you created it and then control if it equals the title.
[safariBtn setTitle:#"Safari" forState:UIControlStateNormal];
then control
-(void)changeButton:(NSString*)button withURL:(NSString*)url {
timetableURL = url;
UIImage *btnImage;
if ([button titleForState:UIControlStateNormal] isEqualToString:#"Safari"]) {
btnImage = [UIImage imageNamed:#"browser-button.png"];
[safariBtn addTarget:self action:#selector(onSafariButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([button titleForState:UIControlStateNormal] isEqualToString:#"Timetable"]) {
btnImage = [UIImage imageNamed:#"browser-timetable.png"];
[safariBtn addTarget:self action:#selector(onTimetableButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
[safariBtn setImage:btnImage forState:UIControlStateNormal];
[btnImage release];
}
Change
[button isEqual:#"Browser"]
To
[button isEqualToString:#"Browser"]
isEqual is testing for object equality, not string equality.
EDIT: OK, you say your button is inside a toolbar. In this case I assume you have added it as a customview to a UIBarbuttonItem? You need to go through the bar button item and set the image property on the custom view of the bar button item, I think (though I'm not sure) once you've added a button as a custom view, the bar button item makes it's own copy so you are not talking to it any more via safariBtn.
You can access the toolbar items via
for (UIBarButtonItem *barItem in self.toolbar.items)
Assuming this code is in your viewController. Your safariBtn will then be accessible via barItem.customView
I created a UISwitch using this code...
UISwitch *switch = [[UISwitch alloc]initWithFrame:CGRectMake(110, 230, 60, 60)];
[window addSubview:switchView];
[switchView release];
The created button will be....
The default properties are,
It contains "ON" & "OFF" states
The OFF button is white & the ON button is in blue color
I want to create a customized switch, so that the background color & text in the switch should be changed. Is it possible? Please explain in detail.
Thanks in Advance,
Rajkanth
You can not modify UISwitch control unless and until you write your own control,
But best way so far, you can used UISegmentControl and handle event on it to switch the on.png and off.png images.
UISegmentedControl* switchView=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:#"On",#"Off",nil] autorelease]];
[switchView setFrame:CGRectMake(20,365,140,28)];
switchView.selectedSegmentIndex=0;
switchView.segmentedControlStyle=UISegmentedControlStyleBar;
[switchView setImage:[UIImage imageNamed:#"onSelected.png"] forSegmentAtIndex:0];
[switchView setImage:[UIImage imageNamed:#"off.png"] forSegmentAtIndex:1];
[switchView addTarget:self action:#selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView=switchView;
and write checkOnOffState method code like this-
-(IBAction)checkOnOffState:(id)sender{
UISegmentedControl* tempSeg=(UISegmentedControl *)sender;
if(tempSeg.selectedSegmentIndex==0){
[tempSeg setImage:[UIImage imageNamed:#"onSelected.png"] forSegmentAtIndex:0];
[tempSeg setImage:[UIImage imageNamed:#"off.png"] forSegmentAtIndex:1];
}
else{
[tempSeg setImage:[UIImage imageNamed:#"on.png"] forSegmentAtIndex:0];
[tempSeg setImage:[UIImage imageNamed:#"offSelected.png"] forSegmentAtIndex:1];
}
}
I used this solution: UICustomSwitch: it works customizing a UISlider and setting a max valure of 1.
You can change the images of your switch, the right / left text and use it with a unique color on background (if you don't want to use images).
The only change I did was about the name: UI is reserved for Apple classe, so I changed it for my own.
I am using a UISegmentedControl with some custom images:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"0.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"1.png"] atIndex:1 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"2.png"] atIndex:2 animated:NO];
segmentedControl.frame = CGRectMake(0, 0, 90, 30);
[self.view addSubview:segmentedControl];
[segmentedControl release];
This part works fine. But, it still uses Apple's styling for the control and just adds my images over it. Is there a way that I don't have to use Apple's styles, and customize the conrol with my own images with no background? I would also like to have my own "selected" state images.
Possible?
Nic,
After playing with this for a while, I decided to simply "roll my own" in a way.
I made some button images in photoshop and got everything looking like a segmented control. Then, I set a different image for the "selected" states of all the buttons. When one button was pressed, I called setSelected:NO to the others. Then, I handled whatever I needed to do.
I'd love to hear other solutions.
In addition to DexterW's answer. I had came to same decision - to mimic UISegmentedControl using UIButtons only.
However its not a simple task to mimic UISegmentControl. I tried to use Selected state of a UIButton but UIButton wont work as expected if set same settings (bg image, font color) for Hightlighted and Selected states. In such case when segment looks selected its still "pressable" visually. So, to avoid it and make buttons set acting more like UISegmentedControl I didn't use a selected state at all. Instead Id decided to change appearance of selected button in code so that normal and highlighted states are the same visually for selected button and different for unselected one.
Assume I have 3 buttons segmentedControl (LeftSgm | MiddleSgm | RightSgm) and for normal state Im using bg picture named "segment_default" and font color white and for selected state its bg picture named "segment_active" and black font. So in onTouchDown:
-(IBAction)onTopMenuTap:(id)sender
{
int newSelectedSegmentIndex;
if (sender == btnLeftSgm)
newSelectedSegmentIndex=0;
else if (sender == btnMiddleSgm)
newSelectedSegmentIndex=1;
else if (sender == btnRightSgm)
newSelectedSegmentIndex=2;
else
return;
NSLog(#"Tapped segment index %d while old one is %d",newSelectedSegmentIndex,selectedSegmentIndex);
if (newSelectedSegmentIndex==selectedSegmentIndex)
return;
[self handleSegmentAppearenace:newSelectedSegmentIndex];
//do whatever its need to be done
...
}
and the called method is
-(void)handleSegmentAppearenace:(int)newSelectedSegmentIndex
{
if (selectedSegmentIndex==0){
[btnLeftSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_default_left"] forState:UIControlStateNormal];
[btnLeftSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btnLeftSgm.highlighted = NO;
}
else if (selectedSegmentIndex==1){
[btnMiddleSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_default_center"] forState:UIControlStateNormal];
[btnMiddleSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btnMiddleSgm.highlighted = NO;
}
else if (selectedSegmentIndex==2){
[btnRightSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_default_right"] forState:UIControlStateNormal];
[btnRightSgm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btnRightSgm.highlighted = NO;
}
if (newSelectedSegmentIndex==0){
[btnLeftSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_active_left"] forState:UIControlStateNormal];
[btnLeftSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnLeftSgm.highlighted = YES;
}
else if (newSelectedSegmentIndex==1){
[btnMiddleSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_active_center"] forState:UIControlStateNormal];
[btnMiddleSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnMiddleSgm.highlighted = YES;
}
else if (newSelectedSegmentIndex==2){
[btnRightSgm setBackgroundImage:[UIImage imageNamed:#"segmented_control_active_right"] forState:UIControlStateNormal];
[btnRightSgm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnRightSgm.highlighted = YES;
}
}
All three buttons are bound in IB to corresponding properties (btnRightSgm etc). Also for "touch up event" onTopMenuTap is bound.
Cant say its a best idea but it works perfectly for me.
I am trying to make a standard check box for my iPhone app from a UIButton with a title and image. The button image changes between an "unchecked" image and a "checked" image.
At first I tried subclassing UIButton but UIButton has no -init*** method to use in my -init method.
What is the best way to do this?
Thanks in advance.
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
Set the images in the button:
[button setImage:uncheckedImage forState:UIControlStateNormal]
[button setImage:checkedImage forState:UIControlStateSelected]
Now all you need to do is:
button.selected = state
and the correct images will display.
All you need to do is set 2 different images for the states UIControlStateNormal and UIControlStateSelected, then in your selector, changed the selected property of the button.
Here is a working example (replace image names with your own):
- (void)loadView {
// ...
UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[chkBtn setFrame:CGRectMake(0, 0, 300, 25)];
[chkBtn setImage:[UIImage imageNamed:#"UNCHECKED.png"]
forState:UIControlStateNormal];
[chkBtn setImage:[UIImage imageNamed:#"CHECKED.png"]
forState:UIControlStateSelected];
[chkBtn addTarget:self
action:#selector(chkBtnHandler:)
forControlEvents:UIControlEventTouchUpInside];
// Optional title change for checked/unchecked
[chkBtn setTitle:#"I am NOT checked!"
forState:UIControlStateNormal];
[chkBtn setTitle:#"I am checked!"
forState:UIControlStateSelected];
[self.view addSubview:chkBtn];
[chkBtn release], chkBtn = nil;
// ...
}
- (void)chkBtnHandler:(UIButton *)sender {
// If checked, uncheck and visa versa
[sender setSelected:!sender isSelected];
}
For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:
https://github.com/Brayden/UICheckbox
I have used M13Checkbox in one of my projects. Works ok.
https://github.com/Marxon13/M13Checkbox
Did you try overriding the initWithCoder method, just in case it is loaded from a nib somehow?
UIImage* nonCheckedImage=[UIImage imageNamed:#"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
UIImage* CheckedImage=[UIImage imageNamed:#"ic_check_box_black_48dp.png"];//[UIImage init
//ic_check_box_black_48dp.png
[_checkBox setImage:CheckedImage forState:UIControlStateSelected];
[_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
}
- (IBAction)checkBoxToggle:(id)sender {
_checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL
}
the image you can use google icon
Try this:-
-(IBAction)clickCheckButton:(UIButton *)sender {
if (sender.tag==0) {
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}else
{
sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
}
Why not use a switch - UISwitch? This is used to display an element showing the boolean state of a value.