NSScrollView position is always bottom [duplicate] - xcode

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.

Can't set constraints of UItableView's tableHeaderView height by autolayout in Storyboard

I want the screen only shows one tableveiwCell with headerView.I added a UIView as header view on the top of UITableView with "Size classes" in storyboard(Just drag a UIView on the top of the UITableView), it can compatible with all devices screen size in this way.
So change header view's height by constraints is what i want to. but when i try to do that, i cant set constraints for the headerView(Xocde doesn't enable me select any constrains in storyboard, Image 2 below). As below.
Any ideas, thanks!
The project code is here: https://github.com/williamhqs/AutoLayoutTableViewHeaderView
EIDT:
Seems it still can't be set in storyboard.
Then i will have to change table header view's frame by code then update constraints.
UIView *v = self.tableView.tableHeaderView;
CGRect fr = v.frame;
fr.size.height = [UIScreen mainScreen].bounds.size.height -100;
v.frame = fr;
[self.tableView updateConstraintsIfNeeded];
Then i will have to change table header view's frame by code then update constraints.
UIView *v = self.tableView.tableHeaderView;
CGRect fr = v.frame;
fr.size.height = [UIScreen mainScreen].bounds.size.height -100;
v.frame = fr;
[self.tableView updateConstraintsIfNeeded];
i think that UIView doesn't have any superview So that it is not showing the constraints for view.

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

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)

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