Currently now i want to create a round corner NSImageView,i am a newb,how to ?
Asked
Active
Viewed 7,036 times
2 Answers
31
I don't know if this will work so please try it and we'll cross our fingers. On the iPhone you can use the CALayer
of any UIView
(the NSView
counterpart in iOS) to get rounded corners. Based on the reference docs it appears that NSView
supports this but again you'll have to try it. Please let me know if it works.
NSImageView *view = your view;
[view setWantsLayer: YES]; // edit: enable the layer for the view. Thanks omz
view.layer.borderWidth = 1.0;
view.layer.cornerRadius = 8.0;
view.layer.masksToBounds = YES;
You can also modify the borderWidth and borderColor
properties.

Axel Guilmin
- 11,454
- 9
- 54
- 64

par
- 17,361
- 4
- 65
- 80
-
7This only works for layer-backed (or layer-hosting) views. You have to call [myView setWantsLayer:YES] first. – omz Jan 14 '11 at 08:12
-
I tried that and it worked nice but when I resize my window the image is redrawn without the cornerRadius, any ideas how to prevent this? – Jeena Dec 01 '11 at 22:03
-
It's important to `#import
`, I didn't think about it and couldn't have found the reason why does this not work. Not everything is just fine :) – Michał Siwek Apr 18 '14 at 12:34 -
1I am sorry, but for for NSImageView, this answer doesn't work at all. I have a normal NSImageView embedded in some other NSView. I set it to "want CALayer" both in .xib and in code (as indicated above). I set it to keep its aspect-ratio, and the pixel leading/traling/top/bottom space relative to superview - the round corners never show. not initially, and not while I resize the window (which resizes the imageView by itself) – Motti Shneor Jan 02 '16 at 20:27
2
This is what I used in Swift:
imageView.wantsLayer = true
imageView.layer?.borderWidth = 1.0
imageView.layer?.borderColor = NSColor.red.cgColor
imageView.layer?.cornerRadius = 25.0
imageView.layer?.masksToBounds = true

madx
- 6,723
- 4
- 55
- 59