iPhone Buttons Question - objective-c

Once I tap the button, its default highlight color is blue. How can you change the highlight color of a button. Either in interface builder or by code.

In IB when you select the Button Attributes of your UIButton choose "Highlighted State Configuration" (instead of "Default State Configuration") and select an image for the Background.
Or in code: [myButton setBackgroundImage:[UIImage imageNamed:#"imagename"] forState:UIControlStateHighlighted];
So you rather set an image, than set a color.
You might want to look into code examples of creating your custom UIButton ([UIButton buttonWithType:UIButtonTypeCustom]), using UIImage's stretchableImageWithLeftCapWidth:topCapHeight: instance method.

Related

Transparent NSButton shown with unwanted backgroundColor

So I have an NSView in which I created some borderless buttons programmatically.
Although I set their backgroundColor property to desired color, they show up like they have a darker shade.
I guess that is the background of Label, not of Button, which does not appear when users choose to reduce transparency in System Preferences.
In that case, I have solved by this line of code:
myButton.appearance = NSAppearance(named: .aqua)
here How I would do it as I mentioned in comment.
The custom view is connected as iboutlet and given green color.
Label has by default no back ground color.
The button has no border and of type Round Rect as you see in screen shot.
The blue color around button is the selection/ active control, but you can get rid of that too.
self.testBtn.focusRingType = NSFocusRingTypeNone;
edit add new image showing scroll bar:
//set background color
button.backgroundColor=[UIColor clearColor]
//then set tint color of button
[btn setTintColor:[UIColor clearColor]];

Button background color is repeating in the uicollectionview cell

I have a like button on my cell (that has been connected to the CustomCVCell.h file) and I need to change the background of this button when it gets pressed.For that I written this code.
Collectionviewcell.h
(IBAction)secondlikebuttn:(id)sender;
In collectionviewcell.m
- (IBAction)secondlikebuttn:(id)sender {
UIButton *buttonObj = (UIButton*)sender;
[buttonObj setBackgroundImage:[UIImage imageNamed:#"like_icon_inst1_red"]
forState:UIControlStateNormal];
_commview.hidden=YES;
_shareview.hidden=YES;
_likeview.hidden=NO;
}
when click on the button the background image of the button at selected idexpath is changing but the problem is it is reflecting the change in other indexpath also.
Thanks for quick responses.

Change background color of UIButton while highlighted

Is there any way to change the background of a uibutton while it is highlighted and then have it revert back to original state?
You can set an image in the utilities panel at the right-side of your screen.
Or use this code:
[button setImage:[UIImage imageNamed:#"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

How to change background image on a button , iOS?

I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that?
I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:#"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button?
Thank you very much for reading my post :D
you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:#"myBackgroundImage.png"] forState:UIControlStateHighlighted];
A button has two properties image and backgroundImage..
For setting image use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];
First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:#"imagename.type"] ForState:UIControlStateNormal];
One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom".
This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.
Hope this helps.
You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).
Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).
Setbutton
if u need image while clicking
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateHighlighted];
or if u want image after Selecting it
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateSelected];
and on that onclick function mention
as
snapshotCheckbox.Selected=YES;
To set an image on a button, just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

How to remove effect from a button?

In my app for iPad. First I have a welcome screen. Screen has a custom button with an image. When I press that button, it shows some gray effect before moving to the next screen. I don't want that effect to appear. How can I remove that gray effect?
Regards
PC
That is the highlighted state of the UIButton. Per the UIButton Class Reference you can call [button adjustsImageWhenHighlighted:NO]; to remove it.
Set the image for the button to nil for the Highlighted state.
[mybutton setImage:nil forState:UIControlStateHighlighted];