How to create a gray overlay view on a UIButton that contain an image, but to make the overlay to be shown only on the image itself, not on the whole button so just the image will be gray color.
Is it possible?
This is the button as it is:
This is with overlay:
This is how I want it to be:
Thanks!
Just set the image you want for the highlighted state. You can do this in a XIB or on your storyboard by setting the state to "Highlighted":
and then setting the image:
OR
Do it through code like this:
[theButton setImage:theImage forState:UIControlStateHighlighted];
The image can be quickly duplicated and converted to black-and-white with Photoshop or GIMP, etc. Changing the image to black-and-white programmatically is way more work than it's worth.
Related
When I use a NSButton with style 'Round Rect', type 'Toggle' and an image, the image and title will be nicely rendered in blue, when the State is set to On.
However, when I also add an alternate image, this highlight effect is gone.
Is there a simple way to fix this?
I want to achieve the same effect as the small tab bar icons on top of the Inspector in Xcode: blue when selected, using an alternate image (slightly bolder), black when not selected, using the default image.
This is the expected behavior. The system only transforms your template to apply the visual highlight if you don't supply your own alternate (active / lit / on) version.
While waiting for a more elegant solution, I'm using this workaround:
-(IBAction)handleClick:(id)sender
{
NSButton *button = sender;
button.image = [NSImage imageNamed:[button.identifier stringByAppendingString:button.state == NSOnState ? #"On" : #""]];
}
I toggle in code between the standard image and the alternate image, using the identifier of the control as a base name for the image. A bit ugly, but it works. The image on the button is now blue highlighted while showing the (slightly bolder) alternate image, when in 'On' state.
To NOT use an alternate image and get an NSButton (macOS 10.13) with Blue Tint when ON and not when OFF like some of the buttons in Xcode toolbar, I had to do the following:
png in asset catalog with transparent background and white line drawing.
png in asset catalog set to render as "template" (see right sidebar).
Set only 1 image. Do not use an alternate image or it no worky.
bezelstyle = NSTexturedRoundedBezelStyle.
buttontType = NSToggleButton.
I also change controlTint in the button action handler for state.
My UIbutton was stretching my images I was assigning to it with
[self.BTN_pic_1 setImage:myPIC forState:UIControlStateNormal];
I used this code to the correct resizing mode like this
[self.BTN_pic_1 setContentMode:UIViewContentModeScaleAspectFit];
The aspect fit only works once I press the button. I want the Aspect Fit to start working as soon as I load the image.
How can I get the button to do that?
Use the imageEdgeInsets to control how the image is stretched.
or
Set the contentMode of the button's imageView instead of the button's.
[self.BTN_pic_1.imageView setContentMode:UIViewContentModeScaleAspectFit];
Disclaimer:
There are many posible answers to this, you'll probably need to be more specific before we can give you a definitive answer.
It is not the button's content mode you want to fix; it's the content mode of the button's imageView. That is what is holding the image you assign when you call setImage:forState:.
I have settings View which is a View Object under self.view.
I added the background image as: self.settingsView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"settings_bg__650x740.png"]];
The problem is, it is having extra images at the bottom. This only happens in iOS 7.
Here's the screenshot:
Please, help me to solve.
The image is being tiled. Why are you creating a tiled image and using it as a background color? Just put the image in a UIImageView and add the image view to the view.
try resize your image before adding to background.
You can find answer in here
Stretching a image in iOS using colorWithPatternImage
the settings view's bound size is higher than the background color referred image. so, the image will tile to fill the settings view.
I have an image with irregular shape. I set it as a background image of an UIButton.
Now, I use OBShapedButton library to enable touch only where image is non-transparent. But, for some reason if I click on transparent image area, when view appears in screen, then it responds to click, whereas it should not.
I made some area of image transparent using adobe cs4 photoshop. Now, I want to try making it transparent in xcode only. May be it helps.
To have an idea of image shape I am attaching a sample image.
Does anyone know how to do that?
I have an image (3width x 15height, the first and last pixels are rounded on the corners), and I want to create a button (the height of the button is always the same, 15px, but the width changes) that uses this image no matter what size it is, kinda like UIEdgeInsets on iOS.
I've tried to use [[button cell] setImageScaling:NSImageScaleAxesIndependently]; but the image gets distorted and loses quality.
How can I do this on OSX? Thanks!
You could probably create a custom subclass of NSButtonCell and use the Application Kit's NSDrawThreePartImage() function in the cell's drawInteriorWithFrame:inView: method.
For more info on how to use NSDrawThreePartImage(), see Cocoa Drawing Guide: Drawing Resizable Textures Using Images.
If it's possible to set this in graphical interface > Attribute Inspection > Button section Background