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];
Related
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];
I have a requirement in a project that the UINavigationBar Back button should never have text in it, it should always just be a left arrow.
By default iOS is going to insert the title of the previous controller in there. Is there any way I can stop this from happening across the whole app?
(I know I can do this screen by screen, but I'm working on an existing app with A LOT of screens it and this would be a big job)
You can always set an image of an arrow to left bar button of navigation bar
// ADDING IMAGE TO BUTTON
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setFrame:CGRectMake(0,0,30,30)];
[refreshButton setImage:[UIImage imageNamed:#"arrow_image.png"] forState:UIControlStateNormal];
refreshButton.userInteractionEnabled=NO;
// ASSIGNING THE BUTTON WITH IMAGE TO LEFT BAR BUTTON
UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
self.navigationItem.leftBarButtonItem = refreshBarButton;
You will have to write this in each view controller in order to disable default left bar button.
You can't stop it from happening across the whole app, you'll have to set it manually in each controller. You could use a category on UIViewController and call that method in each controller, which will get you down to 1 line of code that doesn't have to change if you change your approach. Still sucks, I know. Also, you will probably have issues with Apple if you do that. We tried that in one of our apps and when I showed it to the Apple guys at WWDC '13 they flat out told me they would reject the app if I submitted it that way. YMMV
I've created an NSButton (see code below). The button displays the way I want (borderless, with image above text). If I click the text portion of the button the button will press down and call it's action. If I click the image portion of the button then absolutely nothing happens. How can I make the image portion of the NSButton clickable? I'm sure I can subclass the NSButtonCell to make this work, but I don't understand why that would be necessary.
NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect];
[button setBezelStyle:NSRegularSquareBezelStyle];
[button setBordered:YES];
[button setImagePosition:NSImageAbove];
[button setButtonType:NSMomentaryChangeButton];
[button setImage:[NSImage imageNamed:#"myImage"]];
[button setTitle:#"Button Title"];
[button setTarget:self];
[button setAction:#selector(buttonAction:)];
The image on the button itself. No need to make it clickable. Your click event will be caught by buttonAction.
You only need to show the animation that image got clicked. For that, you need another inverse image. And track the mouse down event, when you click down show the other image, when you release mouse (mouse up) show the default image.
This will give the feel of click on the image.
Maybe some other view overlaps your button's image? In code above, of course if you provide appropriate button frame, all works fine (for both, bordered and borderless button). Try it in clean project.
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.
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.