I have two buttons in each row of a tableview. One is labeled "have it" the other "want it" Each button starts off at 20% opacity when the app starts. When one button is tapped the opacity is set to 100% . I need logic so that if one button is set to 100% opacity and the other one set at 20% is tapped, the first button needs to be set to 20% and the second button to 100% (so the opacity needs to be reversed).
Each button has it's own action that is run when pressed. I can access the button that is pressed and set the opacity with (UIButton *senderButton = (UIButton *)sender). However I need to set the opacity of the other button as well. How can access the other button (the one that was not pressed) inside of my action/function that is called when one is pressed? Thanks!
You can create an outlet for each button. So that you can set its property from any where within its container class.
if I correct understand your question, you can declare your buttons in header-file like this:
#interface myController : UIViewController
{
UIButton *b1;
UIButton *b2;
}
tmen in m-file (in viewDidLoad) you can set this buttons with one selector and different tags: (for more information about creation buttons: How do I create a basic UIButton programmatically?)
-(void)viewDidLoad
{
[super viewDidLoad];
b1 = [UIButton buttonwithType:UIButtonTypeCustom];
[b1 addTarget:self withAction:#selector(clickINMyButtons:) forState:UIControlTouchUPInside]; // sorry, I don't remember correct syntax, i'll correct this some later if you needed in it.
b1.tag = 1;
b1.frame = CGRectMake(0,0,12,12); //example
[self.view addSubView:b1];
}
alike declare b2 with different:
b2.tag = 2;
So, then you implement your selector with changing opacity:
-(void)clickINMyButtons:(UIButton *)sender
{
if (sender.tag == 1)
{
sender.alpha = 1; // or b1.alpha = 1;
b2.alpha = 0.2;
}
else if (sender.tag == 2)
{
sender.alpha = 1; // or b2.alpha = 1;
b1.alpha = 0.2;
}
}
Related
I have a button that you can press and the text of that button will change. I want to be able to detect when the user clicks the button again, so that the button text will change back to its original text. How would I be able to do that? Here is the code I have so far.
//Set Text and alignment for the buttons
[nocavities setTitle:#"No cavities\n5 points" forState:UIControlStateNormal];
nocavities.titleLabel.textAlignment = NSTextAlignmentCenter;
-(IBAction)nocavitiesaction:(id)sender {
[sender setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
}
I feel like in the IBAction I should change the state to
UIControlStateNormalPressed
but I'm not sure.
You can set a counter inside the IBAction
Something like this:
-(IBAction)nocavitiesaction:(id)sender {
static int count = 0;
count ++;
if(count % 2 != 0){
//Set title 1
}else{
//Set title 2
}
}
Open xib file and select the button.
Under the attributes inspector you will find "State Config", select "Default" from dropdown.
Change the title of button to "No cavities\n5 points"
Change State Config to "Selected"
Change title to "Whenever you visit our office cavity-free, you will receive 5 points!"
Now toggle these titles by.
- (IBAction)nocavitiesaction:(id)sender
{
UIButton * button = sender;
button.selected = !button.selected;
}
like this.
declare global int flag=1;
-(IBAction)methodName:(id)sender
{
if(flag==1)
{
//set title 1
flag=2;
}
else
{
//set title 2
flag=1
}
}
There's no UIControlStateNormalPressed state, but there is a UIControlStateSelected. You can set a title for both the selected and normal states, then manually set the UIButton to its selected state like so:
- (void)viewDidLoad {
[self.button setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
[self.button setTitle:#"No cavities\n5 points" forState:UIControlStateSelected];
}
- (IBAction)nocavitiesaction:(UIButton*)sender {
sender.selected = !sender.selected;
}
Note: I've changed the parameter from id to UIButton to avoid having to convert id to UIButton to check its selected state. And the UIButton should be of type "Custom" if you'd like to prevent the background tint while selected.
I am making an iOS 7 app, I know that Apple's new design guidelines call for a bunch of flat design minimalist stuff, but this app is not really targeted at the tech-savvy crowd, so apple's guidelines pose a problem. I have a regular button, with just text in it, and I would like to put an outline around it, and I would also like for the button to react to being pressed, so that people actually know it is a button, and so that they do not freak out when the button takes them to a different app? So how do I
Put an outline around a regular iOS button?
Make a regular iOS Button give some simple visual feedback to being pressed?
Simplest way: make the UIButton's type be "System", rather than "Custom". A system button's image and/or text will highlight when touched.
You should do this in Interface Builder, by changing button's "Type" to be "System"
However, if you need to do it programmatically, you can do:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
As for the UIButton's border, you can do:
- (void)viewDidLoad {
[super viewDidLoad];
self.button.layer.cornerRadius = 5;
self.button.layer.borderColor = [UIColor blackColor];
self.button.layer.borderWidth = 1;
}
If you are using a storyboard (interface builder) for designing your app it's quite easy:
Create a subclass of UIButton. Let's call it XYBorderButton.
In XYBorderButton.m add the methods:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self makeBorder];
}
return self;
}
- (void)makeBorder {
self.layer.cornerRadius = 10.0;
self.layer.borderColor = [UIColor blueColor];
self.layer.borderWidth = 1.0;
}
Then in interface builder select the button and change its class to XYBorderButton
You can give visual feedback for example by changing the button's background color and/or by changing its font color.
Setting these attributes is quite easy with Interface Builder:
Just select the button, then choose the state "Highlighted" in the state config dropdown menu and set the background color and font color as desired.
extension UIButton {
func provideVisualFeedback4press()
{
backgroundColor = cyan
alpha = 0
UIView .animate(withDuration: 0.1, animations: { [weak self] in
guard let s = self else {
return
}
s.alpha = 1
}, completion: { [weak self] completed in
if completed {
guard let s = self else {
return
}
s.backgroundColor = UIColor.white
}
})
}}
usage:
#objc func backAction(_ sender:UIButton!)
{
sender.provideVisualFeedback4press()
If you set the text or image properties of a UIButton, it'll automatically give feedback when pressed (the font color and image will go darker). However if you simply placed the button on top of some other controls, then you'll have to wire up to the Touch Down event and manually change the appearance to any control you want.
I am creating an iOS app with a seating plan layout.
Trying to use an object-oriented approach, I created a class for TableLayoutObjects as they have different properties.
And to lay these TableLayoutObjects out on the screen I am representing them as UIButtons that are created as I loop through the array of TableLayoutObjects.
- (void) loadTables
{
for (TableLayoutObjects *layoutObjs in arrTableLayoutObjects)
{
if ([layoutObjs.shape isEqualToString:#"r"]) {
// rectangle
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
......
if(layoutObjs.isInteractable) {
[button addTarget:self action:#selector(tableTouchedDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(tableTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
} else {
// text only. use label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(layoutObjs.posX, layoutObjs.posY, layoutObjs.width, layoutObjs.height)];
......
}
}
}
My event handlers look like the below for now.
// reverts back to original color and perform other instructions
- (void) tableTouchedUpInside:(UIButton *) button
{
button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor;
}
My question is: how do I identify the UIButtons to their TableLayoutObjects? In the event handler after I change the colour of the button, I will also want to get or set some properties of the selected TableLayoutObjects. How can I do that?
I think your example is a perfect fit for implementing a UICollectionView. Solution with the buttons is less clean and more complex.
You can set the tag of the button to the index into the arrTableLayoutObjects array of the associated item.
Alternatively, create a custom class which takes the table as a parameter and is the target of the button. This object now has direct access to the button and the table item.
I am designing a Music app for iOS 7 and I want to put the "AirPlay" route selector button directly in my app. I am able to get the button placed just fine, however it doesn't show up because the icon is white and my background is white.
Is there a way to change the color of the Route Button?
Here is the code I'm using to create the button.
self.airPlayButton = [[MPVolumeView alloc] initWithFrame:CGRectZero];
self.airPlayButton.showsVolumeSlider = NO;
[self.airPlayButton sizeToFit];
self.airPlayButton.backgroundColor = [UIColor myGreenColor];
[self addSubview:self.airPlayButton];
Basically the picture below is what I want, except I want the icon green instead of just it's background.
in reviewing Adams answer I like more clarity in that task. So I safe-guarded that code a bit:
Objective-C:
for( UIView *wnd in volumeView.subviews ) {
if( [wnd isKindOfClass:[UIButton class] ]) {
UIButton *button = (UIButton*) wnd;
UIImage *img = button.currentImage;
UIImage *img2 = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[volumeView setRouteButtonImage: img2 forState:UIControlStateNormal];
break;
}
}
Swift:
for view in volumeView.subviews {
if view.isKindOfClass(UIButton) {
let buttonOnVolumeView : UIButton = view as! UIButton
volumeView.setRouteButtonImage(buttonOnVolumeView.currentImage?.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
break;
}
}
Now it reacts on the tintColor property of volumeView and if Apple decides to add another button or change the sequence this code will still work.
To expand on lanbo's answer, you can also get the original image of the Route Button and create a copy that uses the UIImageRenderingMode.AlwaysTemplate rendering mode. That way it heeds the current tintColor.
In Swift:
let volumeView = MPVolumeView()
if let routeButton = volumeView.subviews.last as? UIButton,
let routeButtonTemplateImage = routeButton.currentImage?.imageWithRenderingMode(.AlwaysTemplate)
{
volumeView.setRouteButtonImage(routeButtonTemplateImage, forState: .Normal)
}
Create your own image and Try
setRouteButtonImage:forState:
Assigns a button image to the specified control states.
- (void)setRouteButtonImage:(UIImage *)image forState:(UIControlState)state
Parameters
image - The image to associate with the specified states.
state - The control state with which to associate the image.
Discussion
Use this to customize the appearance of the route button when it is enabled, disabled, highlighted, and so on.
Available in iOS 6.0 and later.
How can I change the image of the UIButton three times and also get the state or set a value that later i can use as my reference.
For example, The initial image of UIButton is white when first click it will turn to yellow then third clicked it will be green then if its green and clicked again it will return to white. i also need to set a value from different states.
And this will be applied to 10 individual 'UIButton'.
Scenario, UIButton will be used to display the state of the watches, initial value is white which mean -new when clicked it will turn to yellow which mean used and when clicked again it will turn to green which mean first class watch.
just a sample scenario only.
you could do something like this:
-(void) buttonPressed: (id) sender {
if([sender isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)sender;
if(button.backgroundColor == [UIColor White])
button.backgroundColor = [UIColor Yellow];
else if(button.backgroundColor == [UIColor Yellow])
button.backgroundColor = [UIColor Green];
else if(button.backgroundColor == [UIColor Green])
button.backgroundColor = [UIColor White];
}
}
havnt tested that at all, but something along those lines should work. treat this as pseudo code