【问题标题】:contentsize and contentOffset equivalent in NSScroll viewNSScroll 视图中的 contentsize 和 contentOffset 等效项
【发布时间】:2011-03-28 08:10:30
【问题描述】:

我正在将应用程序从 Ipad 移植到 mac。 (我知道这听起来很奇怪)

我坚持使用 NSScrollview。请指导我 contentsize , contentOffset 在 NSScrollview 中等效。

【问题讨论】:

    标签: ios iphone macos uiscrollview nsscrollview


    【解决方案1】:
    UIScrollView* uiScroll;
    uiScroll.contentSize;
    uiScroll.contentOffset;
    uiScroll.contentSize = CGSizeMake(w,h);
    uiScroll.contentOffset = CGPointMake(x,y);
    

    =

    NSScrollView* nsScroll;
    nsScroll.documentView.frame.size;
    nsScroll.documentVisibleRect.origin;
    nsScroll.documentView.frameSize = NSMakeSize(w,h);
    [nsScroll.documentView scrollPoint:NSMakePoint(x,y)];
    

    或者甚至更好:

    import AppKit
    
    extension NSScrollView {
        var documentSize: NSSize {
            set { documentView?.setFrameSize(newValue) }
            get { documentView?.frame.size ?? NSSize.zero }
        }
        var documentOffset: NSPoint {
            set { documentView?.scroll(newValue) }
            get { documentVisibleRect.origin }
        }
    }
    

    注意:我使用了“documentSize”(和“documentOffset”),因为“contentSize”与 NSScrollView 的现有属性冲突。

    【讨论】:

    • nsScroll.documentView.scrollPoint 不可用。它应该是[nsScroll.documentView.scrollPoint NSMakePoint(x,y)]
    • @HarryNg 谢谢,我最近对语法进行了现代化改造并造成了这个错误。我现在已经修好了。
    • UIScrollView.contentOffset 设置器和NSView.scroll(_:) 之间存在细微差别:后者使滚动条闪烁。如果有人发现如何不让它们闪烁,请告知。
    【解决方案2】:

    除了来自@aepryus 的行之外,还有一些更有用的行用于获取/设置 macOS 上的滚动偏移:

    //Get the current scroll offset:
    _contentViewOffset = scrollView.contentView.bounds.origin;
    
    //Set the scroll offset from the retrieved point:
    NSPoint scrollPoint = [scrollView.contentView convertPoint:_contentViewOffset toView:scrollView.documentView];
    [scrollView.documentView scrollPoint:scrollPoint];
    

    【讨论】:

      【解决方案3】:

      您需要了解的有关NSScrollView 的所有信息都在文档中提供的Scroll View Programming Guide for Cocoa 中列出。

      虽然看起来没有直接等价物,但UIScrollViewcontentSize 可以比作NSScrollViewdocumentView 的大小,这是作为NSView 提供的可滚动内容到NSScrollViewsetDocumentView:

      setContentOffset: 可以与NSViewscrollPoint: 进行比较,后者使用NSPoint 来指定documentViewNSScrollView 中的偏移量。

      有关详细说明和代码示例,请参阅文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多