How do you get NSScrollView to center the document view in 10.9 and later?

前端 未结 4 1642
北恋
北恋 2020-12-23 23:36

There are lots of examples out there of how to get NSScrollView to center its document view. Here are two examples (which are so similar that somebody is copying somebody

4条回答
  •  一整个雨季
    2020-12-23 23:42

    1) make sure the view (documentView) directly under the clip view has no constraints to the clip view! (if so, check, remove at build time)

    2) subclass NSClipView

    @implementation CenterClipView
    
    - (NSRect)constrainBoundsRect:(NSRect)proposedClipViewBoundsRect {
    
        NSRect rect = [super constrainBoundsRect:proposedClipViewBoundsRect];
        NSView * view = self.documentView;
    
        if (view) {
    
            if (rect.size.width > view.frame.size.width) {
                rect.origin.x = (rect.size.width - view.frame.size.width) / 2.;
            }
    
            if(rect.size.height > view.frame.size.height) {
                rect.origin.y = (rect.size.height - view.frame.size.height) / 2.;
            }
        }
    
        return rect;
    }
    
    @end
    

    3) change the NSClipView to your subclass

提交回复
热议问题