NSButton loses blue highlight effect when setting an Alternate image - objective-c

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.

Related

NSStatusItem Button Title to the Right of the Image

I want an NSStatusItem’s button’s title to appear to the right of its image. Here’s an example, this is the OS X VPN menubar app:
If I set either the title or image for my NSStatusItem’s button, they render correctly. However if I set both, they get superimposed and are horizontally center-aligned.
Is there a way to achieve the side-by-side placement without creating a custom UIView?
This has to do with a change in Yosemite, which deprecated setTitle: and setImage: on NSStatusItem. The documentation isn't super-helpful for what to do instead, however.
I’ve found that you have to set the title and the image on the button property of the NSStatusItem. That’s how I was getting the behaviour described above, with the title and the image centred over each other.
The solution is simple but not immediately apparent. NSStatusBarItem responds to the setImagePosition: method of NSButton. Here’s the code that fixed my issue:
self.statusItem.button.image = [NSImage imageNamed:#"StatusBarIcon"];
self.statusItem.button.imagePosition = NSImageLeft;
Having done this, setting self.statusItem.button.title positions the items correctly.
based on http://tinkker.io/swift/2015/11/10/nsstatusitem.html, in Swift it looks like button.imagePosition = NSCellImagePosition.ImageLeft does it
use
button.imagePosition = NSControl.ImagePosition.imageLeft

Remove UITabBar horizontal separator in iOS7

I want to remove the horizontal separator line between a UITabBar and the rest of the screen. I'm asking the same question as this guy but updated for iOS7.
Setting the background image of my UITabBar doesn't alleviate the problem, nor does setting the background image of the [UITabBar appearance] object.
Is this still possible in iOS7? If so, how?
You could also hide shadow line using this code:
[self.tabBar setValue:#(YES) forKeyPath:#"_hidesShadow"];
Swift
self.tabBar.setValue(true, forKey: "_hidesShadow")
The answer to this is pretty much the same as removing the separator of a navigation bar or a toolbar.
Officially, this is only possible by setting the shadowImage to an empty image. However, a closer look at the documentation, we see that:
For a custom shadow image to be shown, a custom background image must
also be set using the backgroundImage property. If the default
background image is used, then the default shadow image will be used
regardless of the value of this property.
By using a custom background image, you would lose the blurred background translucency.
The separator is a UIImageView that is a subview of the tab bar. You can find it in the tab bar's hierarchy and set it to hidden.
This solution uses a transparent image, so it is more preferable.
Swift
tabBar.shadowImage = UIImage()
Objective-C
tabBar.shadowImage = UIImage.new;

How to set an image to a NSButton to be resized along with the button?

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

Creating an overlay on UIButton image but only on the image itself

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.

UISegmentedControl custom background image

I have UINavigationBar setup as image - some wood texture. I want to insert UISegmentedControl with 4 buttons on that bar. Buttons should have same texture with slightly changed tint.
One solution would be to change tint alpha of buttons background color, so that texture in background can get trough, but as I set alpha for tint in IB it doesn't save value, it always gets back to value 1.
I cant get UISegmentedControl to show background image, so finally gave up and decide to search for help...
Effect that is desired is one in iBooks app, with buttons in Navigation Bar.
Any link or small help would be appreciated..
This post describes the steps involved and has sample code: http://idevrecipes.com/2010/12/13/wooduinavigation/
https://github.com/xhan/PlutoLand/blob/master/PlutoLand/UI/PLSegmentView.h
a custom SegmentView that can specify two images(normal, clicked)
Do not bother with one of those custom segmented control packages if you're on iOS 5 or later.
Per Dylan's suggestion, use the Appearance functionality in iOS 5+.
See the top answer here:
Customizing Left & Right UISegmentedControl Buttons