How do I fix the height of my view after collapsing NSSplitView?

こ雲淡風輕ζ 提交于 2019-12-08 02:48:09

问题


I've got an NSSplitView with an NSScrollView in the bottom view. The problem is when I collapse, and then re-open (un-collapse) the bottom view, the height of the scroll view is beyond the height of that bottom view so the top part of the scoll view is being clipped. I've got my scroll view and my split view set to autoresize in all directions in IB. Do I need to adjust the height of that scroll view after the un-collapse or am I setting a resizing property wrong, or something else? Below is a before and after image of what the clipping looks like.

Before Collapse:

After Collapse and re-open (notice the scroll bar in the bottom view is clipped)


回答1:


I have the same problem. Fixed it using BWToolkit's split views, which allow you to determine the maximum and minimum height for each view.




回答2:


The problem stems from the fact that cocoa autoresizing rules work by scaling deltas from the previous state to the current state. If any of the margins go to 0 they'll never scale back up as the view grows because of the multaplicative nature of the scaling.

The typical approach to working around this is to use the NSSplitView delegate methods to prevent the split view from getting to small and then have it snap shut - which internally keeps the collapsed view at the minimum size.

Here's a link to the split view documentation.

Also, if you think about the user experience, your views probably look really awful when they're sized down below a certain point - views probably start overlapping, and becoming too small to show their content. Adding this snapping-collapsing behavior addresses both problems.

If you want to see an example of this, Mac OS X's Mail.app snaps its inline message view closed when it gets to a certain height. You should mimic that behavior.




回答3:


You could "reset" things via NSUserDefaults, possibly.. There are keys for such things as NSSplitView Subview Frames, etc, to which you can assign coordinates, a la 0.000000, 0.000000, 0.000000, 720.000000, NO




回答4:


While Jon Hess could describe the problem well (as soon as a subview's width becomes 0 the autosizing information gets lost for auto-width elements), the solution is not really given for all cases. Constraining the width did not help in my case, as the subview can be collapsed.

I managed to achieve an acceptable solution, by implementing the splitView delegate method -splitviewWillResizeSubviews: to maintain a minimum width by setting the subview to hidden instead of shrinking it to zero:

- (void)splitViewWillResizeSubviews:(NSNotification *)notification {
        NSUInteger divider = [[[notification userInfo] valueForKey:@"NSSplitViewDividerIndex"] intValue];
        NSView *subview = nil;
        if(divider == SPLITVIEW_DIVIDER_SIDEBAR) {
            subview = (NSView*)[self.splitView.subviews objectAtIndex:SPLITVIEW_SIDEBAR_INDEX];
        }
        if(subview) {
            if(subview.frame.size.width < SPLITVIEW_MINIMUM_SIDEBAR_WIDTH) {
                CGRect correctedFrame = subview.frame;
                correctedFrame.size.width = SPLITVIEW_MINIMUM_SIDEBAR_WIDTH;
                subview.frame = correctedFrame;
                subview.hidden = YES;
            } else {
                subview.hidden = NO;
            }
        }
    }


来源:https://stackoverflow.com/questions/1248809/how-do-i-fix-the-height-of-my-view-after-collapsing-nssplitview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!