Resizing the NSTableView programmatically - objective-c

How to implement the feature of resizing NSTableView created programmatically ? Interface builder should not be used. It should be like click and drag to change the size of the NSTableView. Is it possible? If yes, please help. . . .

I am afraid you need to write a bit of code to make it working. This is how I would do it.
Make a special Resize View that will track the mouse events and call delegate methods providing how the tracking position changes.
- (void)mouseDown:(NSEvent *)theEvent
{
_startPoint = [theEvent locationInWindow];
[_delegate resizingDidStart];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint hitPoint = [theEvent locationInWindow];
[_delegate resizeWithDeltaX:(hitPoint.x - _startPoint.x) deltaY:(hitPoint.y - _startPoint.y)];
}
Put this view in the right bottom corner of the Base View. Set the autoresizing mask so this view always stays in the right bottom corner.
Put the table view along with its scroll view on to the Base View. Set autoresizing mask of the scroll view so its size and width are sizeable.
In the delegate of the Resize View process changes in the mouse position and set the frame of the Base View.
- (void)resizingDidStart
{
_initialRect = [_baseView frame];
}
- (void)resizeWithDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
{
[_baseView setFrame:NSMakeRect(_initialRect.origin.x, _initialRect.origin.y + deltaY, _initialRect.size.width + deltaX, _initialRect.size.height - deltaY)];
}
Of course the scroll view should be under the resize view. You can draw some ind of a handle on the resize view, etc.

Related

How to prevent certain object from scrolling in a UIScrollView

I don't know if this is possible and I highly doubt that it is but I'm wondering if there's a way where I can prevent a button for example from scrolling in a UIScrollView using Objective-C programming?
Sure. Simply update the button's origin as the scroll view scrolls.
In your view controller, implement the appropriate scroll view delegate method. If not done already, setup your view controller as the scroll view's delegate.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
CGRect frame = self.fixedButton.frame;
frame.origin.y = offset.y + 40;
self.fixedButton.frame = frame;
}
This will keep the self.fixedButton button 40 points below the top of the visible portion of the scroll view. Adjust as needed.
The above all assumes the button is a subview of the scroll view.
Of course it may be a lot easier if the button and the scroll view share a common parent view. Then the button isn't a subview of the scroll view and won't scroll at all.

Intercepting UIScrollView Scrolls

I have a UIScrollView and a UITableView inside the UIScrollView.
I would like to intercept scrolling of the UITableView and only allow scrolling if the super view (UIScrollView) have reached a specific contentOffset.
I have created subclass of both UIScrollView and UITableView, how do i catch scrolling event and intercepting the scrolling while the user is still scrolling?
Example of what i'm trying to accomplish:
The UITableView is going to have a header, if i scroll down the header will collapse to 30% of original size and and stay visible at the top. After i have scrolled back to the top of the UITableView i want the header to expand. In other word i want to extend the scrolling of a UITableView with a header that can collapse/expand.
There might be better way to accomplish this, i'm open for suggestions.
There may be a better way, but this solution is working for me.
UITableView (let's call it innerScrollView) inside of UIScrollView (let's call it scrollView) inside whatever the main view is (let's call it view).
Set an outlet in your view controller for the innerScrollView (self.innerScrollView).
Set bounce in XIB or code to: scrollView.bounces = YES and innerScrollView.bounces = NO.
Set scrolling to disabled initially for the innerScrollView.
In code (viewDidLoad:) set scrollView.contentSize.height to view.frame.size.height + the 70% of the header you want to disappear. Resize innerScrollView to fill the resized contentView to the bottom.
Set your view controller as the delegate for scrollView and implement scrollViewDidScroll: as follows:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat maxYOffset = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
CGFloat minInnerYOffset = self.innerScrollView.contentInset.top;
CGFloat currentYOffset = scrollView.contentOffset.y;
CGFloat newYOffsetDelta = currentYOffset - maxYOffset;
CGFloat currentInnerYOffset = self.innerScrollView.contentOffset.y;
if (scrollView.contentOffset.y >= maxYOffset) {
self.innerScrollView.scrollEnabled = YES;
self.scrollView.contentOffset = CGPointMake(0, maxYOffset);
if (currentYOffset != maxYOffset) {
self.innerScrollView.contentOffset = CGPointMake(0, currentInnerYOffset + newYOffsetDelta);
}
}
else if (currentInnerYOffset > minInnerYOffset) {
self.scrollView.contentOffset = CGPointMake(0, maxYOffset);
self.innerScrollView.contentOffset = CGPointMake(0, currentInnerYOffset + newYOffsetDelta);
}
else if (currentYOffset < scrollView.contentInset.top) {
scrollView.contentOffset = CGPointMake(0, scrollView.contentInset.top);
}
else {
self.lowerScrollView.scrollEnabled = NO;
}
}
I just whipped this up and quickly tested it. It all seems to work, but there may be some improvements and tweaks needed. Regardless, this should get you started at least.
Well, you should use the following UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// here perform the action you need
}

Get mouse location from NSScrollView

I have added my NSScrollView over the content view of my NSWindow object. Now I need to know the mouse location over the scrollview.
I have tried the following. But nothing gives the correct location.
- (void)mouseDown:(NSEvent *)theEvent{
NSPoint eventLocation = [theEvent locationInWindow];
NSPoint locationInScroll = [inputScrollView convertPoint:eventLocation toView:nil];
//Both gives the wrong location.
}
The code is correct, assuming that inputScrollView is the document view and the NSScrollView itself.
Another potential problem could be if you change the orientation of the coordinate system in one of the views?

How to add a scroll view to an ios app

I have a tabbed application that I would like to have a scroll view on. I already have a few text fields and labels on that tab. The problem is, the keyboard hides some of the text fields. How would I add a vertical scroll to prevent that?
You can accomplish what you are looking for without a scrollview.
In you delegate method for you textfield, You can change the frame of the viewController's view.
something like
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
CGRect newFrame = self.view.frame;
newFrame.origin.y = - 40; // move the view up to the point your textfield is visible
self.view.frame = newFrame;
}
Then in Set it back
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
CGRect newFrame = self.view.frame;
newFrame.origin.y = 0;
self.view.frame = newFrame;
}
Usually I animate this with 0.33 seconds duration.
The solution requires quite a bit of code, but here's the general idea of what you need:
You will need to add the text fields (and everything else for consistency) to a scrollView.
You need to setup the scroll view to have vertical scrolling space only, but set the scrollEnabled to false so that the user can't scroll it manually.
Then you need to listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and manually scroll it up/down as required.

How to use NSScrollview?

I can't figure out how to actually use NSScrollview. I dragged the scroll view object onto an NSWindow in the interface builder. I then dragged some NSButtons onto the scroll view. My question is:
How do I actually make it scroll down, for example, 2x the original height?
Of course the user can scroll automatically using their UI. I assume what you want to do is to scroll programmatically.
A bit of background: An NSScrollView has a documentView, which is the underlying view that the scroll view shows a part of, and a clipView, which is the view that is shown on the screen. So the clip view is like a window into the document view. To scroll programmatically you tell the document view to scroll itself in the clip view.
You have two options on how to scroll programmatically:
- (void)scrollPoint:(NSPoint)aPoint –– This scrolls the document so the given point is at the origin of the clip view that encloses it.
- (BOOL)scrollRectToVisible:(NSRect)aRect –– This scrolls the document the minimum distance so the entire rectangle is visible. Note: This may not need to scroll at all in which case it returns NO.
So, for example, here is an example from Apple's Scroll View Programming Guide on how to scroll to the bottom of the document view. Assuming you have an IBOutlet called scrollView connected up to the NSScrollView in your nib file you can do the following:
- (void)scrollToBottom
{
NSPoint newScrollOrigin;
if ([[scrollview documentView] isFlipped]) {
newScrollOrigin = NSMakePoint(0.0,NSMaxY([[scrollview documentView] frame])
-NSHeight([[scrollview contentView] bounds]));
} else {
newScrollOrigin = NSMakePoint(0.0,0.0);
}
[[scrollview documentView] scrollPoint:newScrollOrigin];
}