I have an NSView, which I change some properties to give make it a rounded rectangle, with a gray color, and add it to my view (behind everything)
float gray = 60.0f/255.0f;
NSView* background = [[NSView alloc] initWithFrame: self.iconContainer.frame];
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(gray, gray,gray, 1)]; //RGB plus Alpha Channel
[viewLayer setCornerRadius:5.0f];
[background setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
[background setLayer:viewLayer];
// Place view behind all other views
[self.iconContainer addSubview:background positioned:NSWindowBelow relativeTo:nil];
However no matter what I try, that particular view is drawn above everything else.
Usually, this behaviour is caused by the use of Core Animation layers on a subview, while the superview hasn't any.
Enabling Core Animation layers on the superview should fix the issue, as any subviews will then be drawn using Cora Animation layers, making the drawing process respect the order of subviews.
Related
I have a simple view in an iOS application which is causing me some problems.
In this view I have a UIScrollView (full width/height of main view), which is pinned to the top/bottom/leading/trailing of the main view (in IB).
Inside the UIScrollView I have a UIImageView which is pinned top/bottom/leading/trailing to the UIScrollView.
Now, the image inside the UIImageView is almost double the size of the main view, therefore the content mode of the UIImageView is set to Aspect Fit. My problem is that when I run the project, the image is being displayed at its actual size.
The reason the UIImageView is inside the UIScrollView is due to the fact that I want the user to be able to zoom in to the image (hence the reason it is double the size).
I also want the user to be able to rotate the screen as the image may be in portrait or landscape format depending on which image they choose to view in the previous view.
Surely this should be easy enough to do (in IB as well), however I cannot for the life of me figure it out.
You also have to add width and height constraints for UIImageView. It is caused by the fact that by default UIImageView has an intrinsic content size derived from the size of the image.
Following is code snippet for defining those constraints:
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0]];
Even you can do this in IB.
If I understand your question, you want the content to be zoomed out at the start. Assuming that your content does not have the same aspect ratio as the phone (a safe assumption), you can either "letter box" / "curtain" the content, or only zoom out until either the height or width of the image would show on the phone (before aforementioned letter boxing). I chose this latter approach, and set my min zoom to be the greater of min zooms for either dimension.
The code is thus:
// configure image and scroll view for scrolling to extents of actual image
double widthScale = self.view.frame.size.width / image.size.width;
double heightScale = self.view.frame.size.height / image.size.height;
self.scrollView.minimumZoomScale = MAX(widthScale, heightScale);
self.scrollView.maximumZoomScale = MIN(1 / widthScale, 1 / heightScale) / [[UIScreen mainScreen] scale]; // scale to pixel resolution
self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
It assumes that you are using full screen.
If you want the min of either dimension, use this:
self.scrollView.minimumZoomScale = MIN(widthScale, heightScale);
I'm writing a custom NSView that needs to animate its subviews by applying NSAffineTransformation before drawing them.
I am looking for a method that draws the view's subviews so I can override it
in a way like that:
-(void)drawSubview:(NSView*)subview {
[[NSGraphicsContext saveGraphicsState];
[[self affineTrasformForSubview: subview] concat];
[subview drawRect: subview.bounds];
[[NSGraphicsContext restoreGraphicsState];
}
Any suggestions?
for rotating: NSView setFrameCenterRotation:angle
for scaling: NSView scaleUnitSquareToSize:size
for more than rotating and scaling you would need to make your view a layer backed one and mess around with the layer, even though that is against apple recommendation. But I wouldn't worry too much about that and I think it is worth a try.
I have NSView container (with NSImageView and some other custom subviews). How to set its rotation properly? I tried to set angle through setFrameRotation: and set rotation matrix in views layer. But in these cases subview image becomes downscaled and clipped.
Update:
If I set rotation via [myView setFrameRotation: angle]:
almost fine, except text frame (drawing via [NSString drawAtPoint:...] and rotation anchor is at left-bottom corner (I want at bottom-center, [myView setFrameOrigin:...] does nothing)
If I set rotation via myView.layer.transform = CATransform3DMakeRotation (angle, 0, 0, 1):
frame bound remains unrotated and clips subviews (but this approach is more suitable for view container)
I have a simple custom borderless NSWindow subclass which has a rounded rectangle shape.
In the content view of this window, I've added an NSScrollView.
How do I get the NSScrollView to clip its document view to the rounded rectangle shape of the NSWindow?
I've tried subclassing the NSScrollView, overriding drawRect: and adding a clipping path before calling super. I've also tried subclassing the document view and the clip view with the same technique but I cannot get it to clip.
BTW, this is on Lion with the elastic scrolling behaviour.
After much fiddling, I just discovered that NSScrollView's can be made to have rounded corners by simply giving it a backing layer and setting that layer's corner radius provided you also do the same to it's internal NSClipView. Both are required, which now makes sense, since it's the clip view that actually provides the viewable window into the NSScrollView's document view.
NSScrollView * scrollView = ...;
// Give the NSScrollView a backing layer and set it's corner radius.
[scrollView setWantsLayer:YES];
[scrollView.layer setCornerRadius:10.0f];
// Give the NSScrollView's internal clip view a backing layer and set it's corner radius.
[scrollView.contentView setWantsLayer:YES];
[scrollView.contentView.layer setCornerRadius:10.0f];
Even better IMO:
scrollView.wantsLayer = true
scrollView.layer?.masksToBounds = true
scrollView.contentView.wantsLayer = true
scrollView.contentView.layer?.masksToBounds = true
In Swift I have solved like this:
scrollView.wantsLayer = true
scrollView.contentView.wantsLayer = true
scrollView.layer?.cornerRadius = 20.0
scrollView.contentView.layer?.cornerRadius = 20.0
I am trying to make a NSScrollView with clipped corners, similar to the Twitter app:
I have a NSScrollView subclass which I added the following code:
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *pcath = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:kDefaultCornerRadius yRadius:kDefaultCornerRadius];
[path setClip];
[super drawRect:dirtyRect];
}
I expected the content of the NSScrollView to have rounded corners, but it is not respecting the clipped path. How can I do this?
UPDATE & CLARIFICATION
I know how to make a custom NSScroller, I know how to make it transparent overlay. All I am asking is how to make the NSSCrollView clip its corners, including everything it contains. The NSScrollView is inside a NSView which has a background that could change, meaning a view overlay to fake the rounded corners is not an option.
After much fiddling, I just discovered that NSScrollView's can be made to have rounded corners by simply giving it a backing layer and setting that layer's corner radius provided you also do the same to it's internal NSClipView. Both are required, which now makes sense, since it's the clip view that actually provides the viewable window into the NSScrollView's document view.
NSScrollView * scrollView = ...;
// Give the NSScrollView a backing layer and set it's corner radius.
[scrollView setWantsLayer:YES];
[scrollView.layer setCornerRadius:10.0f];
// Give the NSScrollView's internal clip view a backing layer and set it's corner radius.
[scrollView.contentView setWantsLayer:YES];
[scrollView.contentView.layer setCornerRadius:10.0f];
You can apply a mask to a view's layer:
[myScrollView setWantsLayer: YES];
[myScrollView layer].mask = ...;
The mask is another CALayer. So in this case you'd create a CALayer, set its background colour to opaque, set its bounds to match the scrollview, and give it a cornerRadius of, say, 8.0.
The result would be that the scroll view and all its contents would appear to be masked to a roundrect with a corner radius of 8px.
Have you tried overriding
- (BOOL)isOpaque {
return NO;
}
And setting the scroll view's -setDrawsBackground: to NO and just leave the view without clipping and just draw the corners with [NSColor clearColor] since this will also clear the underlying color and simulate a round effect.