In NSScrollView the vertical slider always points to bottom of the view - cocoa

I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.

I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)

Related

When I use a UIScrollView to zoom out an UIImageView, can I get the zoomed ImageView's position?

I init a scrollView's frame according to my imageView's frame, so does its contentSize. Then I set my imageView as his child in order to zoom out and it works well. But, The method below can only just get the (UIView *)view 's relative location to his father(scrollView).Actually, view.frame.origin = (0, 0), which is weird to me. How explain this situation? Can I get the zoomed imageView's absolute position(to rootview) ?
(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
Parameter view
What changes when UIScrollView is scrolling is its contentOffset & bounds not its subviews' frames.

NSScrollView position is always bottom [duplicate]

I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.
I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)

Core animation geometry for layer-backed view

I have a layer-backed view, and want to understand how anchor point works for it and is it allowed at all to work with CALayer geometry properties (instead of using frame property of NSView)
First, quote from documentation:
The default value for anchorPoint is (0.5,0.5) which corresponds to
the center of the layer's bounds
But when I try to change value of anchor point to (0.5,0.5) directly, I got offset. Here is example, red area is the original position (when nothing applied to the anchor point), the button was moved to the bottom-left corner:
When I set anchor point to (0,0) button placed where it should be:
Update:
Here is the code I use to instantiate Button:
self.button = [[NSButton alloc] initWithFrame:NSMakeRect(130, 130, 100, 40)];
self.button.title = #"My Title";
self.button.wantsLayer = YES;
NSLog(#"Button anchor Point is : (%f;%f)", self.button.layer.anchorPoint.x, self.button.layer.anchorPoint.y);
[panelView addSubview:self.button];
What interesting, seems that for layer-backed view's the default anchor point is 0,0:
NSLog(#"Button anchor Point is : (%f;%f)", self.button.layer.anchorPoint.x, self.button.layer.anchorPoint.y);
produces (0.000000;0.000000)
After all, OS X Deployment target is 10.8
Here is quote from Apple documentation on NSView:
When using layer-backed views you should never interact directly with
the layer.
Seems this code cross the boundary of what you can do with layer-backed views.

Reposition an NSBox in the top pane of a horizontal NSSplitView

I the issue I'm having has to do with the coordinate system in Cocoa but I really don't know. This is all happening in the top pane of a horizontal NSSplitView.
Very simply, I'm trying to position one NSBox right below a second one (I load custom views into the boxes - that all works fine). The top box's top-left corner is at the top-left corner of the pane and never changes. If the height of the top NSBox shrinks I want the top of the second NSBox to slide right up below it. Conversely, if the top NSBox's height increases I want the bottom NSBox to slide down.
This code gets called twice. Box is correct (first time top box, second time bottom box) and v is correct (this is the view I'm loading into the box - this works fine and it is what is causing the height to change in the top box).
NSSize destBoxSize = [[box contentView] frame].size; //the size of the box in the view to load the view into
NSSize newViewSize = [v frame].size; // the size of the view to be loaded
float deltaWidth = [horizSplitView frame].size.width - destBoxSize.width;
float deltaHeight = newViewSize.height - destBoxSize.height;
NSRect boxFrame = [box frame];
boxFrame.size.height += deltaHeight;
boxFrame.size.width += deltaWidth;
boxFrame.origin.y -= deltaHeight;
NSLog(#"vc=%# boxFrame x%f y%f h%f w%f", nibName, boxFrame.origin.x, boxFrame.origin.y, boxFrame.size.height, boxFrame.size.width);
// Clear the box for resizing
[box setContentView:nil];
[box setContentView:v];
[box setFrame:boxFrame];
What you want to do is not so hard, but it will need some subclassing. First of all, you need to subclass NSSplitView and either and override either -(void)init or -(void)awakeFromNib to add this line:
[self setAutoresizesSubviews:YES]; //
Then you need to subclass the two boxes and set their auto resizing masks, either in -(void)init or in - (void)viewWillMoveToSuperview:(NSView *)newSuperView.
For the first box you'll probably want:
[newInstance setAutoresizingMask:NSViewNotSizable];
For the second bbox you'll probably want:
[newInstance setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
See also NSView. It takes a bit of experimenting to get the right combination, but then it works quite nicely.

How do I get the inner/client size of a NSView subclass?

I am doing manual layouting for my Cocoa application and at some point I need to figure out what the inner size of a NSView subclass is. (E.g. What is the height available for my child view inside of a NSBox?)
One of the reasons is that I am using a coordinate system with origin at the top-left and need to perform coordinate transformations.
I could not figure out a way to get this size so far and would be glad if somebody can give me a hint.
Another very interesting property I would like to know is the minimum size of a view.
-bounds is the one you're looking for in most views. NSBox is a bit of a special case, however, since you want to look at the bounds of the box's content view, not the bounds of the box view itself (the box view includes the title, edges, etc.). Also, the bounds rect is always the real size of the box, while the frame rect can be modified relative to the bounds to apply transformations to the view's contents (such as squashing a 200x200 image into a 200x100 frame).
So, for most views you just use [parentView bounds], and for NSBox you'll use [[theBox contentView] bounds], and you'll use [[theBox contentView] addSubview: myView] rather than [parentView addSubview: myView] to add your content.
Unfortunately, there is no standard way to do this for all NSView subclasses. In your specific example, the position and size of a child view within an NSBox can be computed as follows:
NSRect availableRect = [someNSBox bounds];
NSSize boxMargins = [someBox contentViewMargins];
availableRect = NSInsetRect(availableRect, boxMargins.width, boxMargins.height);
If you find yourself using this often, you could create a category on NSBox as follows:
// MyNSBoxCategories.h
#interface NSBox (MyCategories)
- (NSRect)contentFrame;
#end
// MyNSBoxCategories.m
#implementation NSBox (MyCategories)
- (NSRect)contentFrame
{
NSRect frameRect = [self bounds];
NSSize margins = [self contentViewMargins];
return NSInsetRect(frameRect, margins.width, margins.height);
}
#end
And you would use it like so:
#import "MyNSBoxCategories.h"
//...
NSRect frameRect = [someNSBox contentFrame];
[myContentView setFrame:frameRect];
[someNSBox addSubview:myContentView];
The bounds property of NSView returns an NSRect with the origin (usually (0,0)) and the size of an NSView. See this Apple Developer documentation page.
I'm not sure (I never had to go too deep in that stuff), but isn't it [NSView bounds]?
http://www.cocoadev.com/index.pl?DifferenceBetweenFrameAndBounds

Resources