I have a question and I didn't find an answer on it yet. I would like to know how to set the background image of a button in your code. I would like to use a value to change the background.
For example: if (value == one) { set background of button code}
I have two backgrounds but when you give a code you can use buttonimage1.png, buttonimage2.png.
Thnx
First you should add IBOutlet for your button, example:
#property(nonatomic, weak) IBOutlet UIButton *bttn;
next, using this outlet you can change background:
if (value == one) {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage1.png"]
forState:UIControlStateNormal];
} else {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage2.png"]
forState:UIControlStateNormal];
}
that's all
Related
I am new to Objective-C so i am mostly using Storyboard, Someone Please tell me how to create a checkbox using Xcode in iOS.
UISwitch is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?
UISwitch is the standard control used in IOS applications for making binary choices but if you want to use checkbox you can create a UIButton
#property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender
and change its background image on click event of UIButton
- (IBAction)CheckBoxClick:(id)sender {
if(!checked){
[_CheckBox setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if(checked){
[_CheckBox setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
also there are more detailed answers to this question at
How to create a simple checkbox in iOS?
and if you dont want to use images you can check the following
Checkbox in iOS application
CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.
There is the working code for checkbox.
create a class level variable and property of button in #inteface
#interface testViewController (){
BOOL checkBoxSelected;
}
#property (weak, nonatomic) IBOutlet UIButton *checkBox;
#end
in viewdidload set images for the button states.
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxChecked.png"]
forState:UIControlStateSelected];
and after create a button action in that button Action.
checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];
Hope it helps
1) Create Prototype cell in UITableView.
2) Add one button inside the cell and set button style to Custom.
3) Set the image for checkbox.
ios language doesn't use checkbox controller.
but you are use checkbox that you are inport two image select & unselect
Step 1> Create button and set image.
Step 2> Create button touch object method and put if condition for check & uncheck.
for example:
- (IBAction)btnLogin:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.tag == 0) {
btn.tag = 1;
//set image checked.
}
else{
btn.tag = 0;
//set image unchecked.
}
}
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:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
I have a UICollectionView with its cells all laid out.
I have this declared as a subview:
#property (nonatomic, strong) UIButton *aButton;
I then have that declared in each cell like so:
if (_aButton == nil)
{
_aButton = [UIButton buttonWithType:UIButtonTypeSystem];
}
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
The button click method is like so:
- (IBAction) aButtonPressed:(UIButton *) sender
{
// Code never gets heree
}
The if(_aButton== `nil) is needed since cells get reused.
How do I make this work now? Thanks.
Add button action code before button added to view.... May be it will work .
// Call to button pressed for button
[_aButton addTarget:self action:#selector(aButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Add in all _aButton info here
[self.contentView addSubview:_aButton];
I think, the way you are initializing the button is going to give you frame as 0,0,0,0 . So first give it a proper frame and title to be visible on the screen.
Then try tapping on that title and see if it works or not.
It's unclear what you trying to achieve here, but the button is not working because you not setting it's position and size on the cell. It could be done by setting frame or layout constraints.
Try to set button frame:
_aButton.frame = CGRectMake(0.0f, 0.0f, 50.0f, 50.0f);
You can also set background color for the button, just to make it visible on the cell:
_aButton.backgroundColor = [UIColor redColor];
Everything else is correct, button should work.
I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
how would is change background image using UIsegmented control ?
i want to change the background image using segment control and after select that image for above process.
Here's a link to the documentation for UISegmentedControl.
Edit
Maybe I had misunderstood your question. If you want to change the background image of a view, you can add your controller as a target for the segmented control.
[segmentedControl addTarget:self action:#selector(updateBackgroundImage:) forControlEvents:UIControlEventValueChanged];
When the UIControlEventValueChanged gets triggered for your control you can use any of this methods to change the background image you want.
- (void)updateBackgroundImage:(UISegmentedControl *)sender {
self.imageView = [UIImage imageNamed:(sender.selectedSegmentIndex ? #"foo": #"bar")];
}
Original Answer
As you'll find it that document you can change the appearance of the control in different ways. A few examples.
-[UISegmentedControl setBackgroundImage:forState:barMetrics:]
-[UISegmentedControl dividerImageForLeftSegmentState:rightSegmentState:barMetrics:]
-[UISegmentedControl setImage:forSegmentAtIndex:]
When the UIControlEventValueChanged gets triggered for your control you can use any of this methods to change the sender's appearance.
You have to add Target to segmented controller like
Then, when you click on any segment your "action" method will get called , change background image or do anything in "action" method
[segmentedControl addTarget:self action:#selector(action:)forControlEvents:UIControlEventValueChanged];
Method implementation should like
-(void)action:(id)sender
{
//code for setting background image.
}
Set the IBAction to your segmentbar with UIControlEventValueChanged event type and based on selected index you can change the image of your imageView
-(IBAction)segmentedChartButtonChanged:(id)sender
{
switch (self.segmentedButton.selectedSegmentIndex) {
case 0:
self.imageView.image = [UIImage imagenamed:#"imageName"];
break;
case 1:
self.imageView.image = [UIImage imagenamed:#"imageName"];
break;
default:
break;
}
}
Try this one.. May be it will help you..
First take UIImageView and set IBOutlet of that and give name like Backgroundimg And also add Segment value change method And also set outlet to Imageview in xib and outlet of method to UISegmentControl.
//.h file
IBOutlet UIImageView *Backgroundimg;
-(IBAction)SegmentChanged:(id)sender;
And just add below code in .m file
//.m file
-(IBAction)SegmentChanged:(id)sender
{
UISegmentedControl *seg=(UISegmentedControl *)sender;
if(seg.selectedSegmentIndex==0)
{
[Backgroundimg setImage:[UIImage imageNamed:#"intro1.png"]];
}
else if(seg.selectedSegmentIndex==1)
{
[Backgroundimg setImage:[UIImage imageNamed:#"intro2.png"]];
}
}
I have written some code which toggles 2 buttons depending on which one is selected. If the UK one is selected it becomes ticked and the BR one becomes unticked, and vice versa. However, this only seems to be the case for the UK button. If I select the BR button than the UK button unticks, the BR button briefly ticks but then it unticks again.
I have linked up my buttons correctly (I have triple checked), and as the BR button briefly ticks it is definitely linked up. The code I am using is below:
.h
#property (weak) IBOutlet UIButton *btUK;
#property (weak) IBOutlet UIButton *btBR;
.m
- (IBAction)changePortal:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.tag == kUKButton)
{
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = YES;
}
else if (button.tag == kBRButton)
{
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = NO;
}
}
I have set break points within the code and have confirmed that both the buttons go in to their relevant sections when clicked. I can also confirm that no other code is using the btUK and btBR variables as I have just written it all.
Both buttons have changePortal set as their action, and the function is only called once per click.
I have also tried cleaning the code but this did not fix my issue.
If anyone can shed any light as to why this is happening then I would be very grateful.
The correct way to set the image of a UIButton is to call setImage:forState:.
So try to alter your code to something like this:
// Follow this pattern for every button image change
[self.btUK setImage:[UIImage imageNamed:#"tick_box.png"] forState:UIControlStateNormal];
Now regarding the imageView property the documentation states that:
The button’s image view. (read-only)
#property(nonatomic, readonly, retain) UIImageView *imageView
Discussion
Although this property is read-only, its own properties are
read/write. Use these properties to configure the appearance and
behavior of the button’s view. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch = YES;
The imageView property returns a value even if the button has not been
displayed yet. The value of the property is nil for system buttons.