Добавить ограничение между содержимым NSScrollView и внешним объектом - PullRequest
0 голосов
/ 09 февраля 2019

Как сделать объект вне блокировки прокрутки на объекте в представлении прокрутки, используя ограничения Auto Layout?

Цель приведенного ниже кода - заставить прямоугольный объект справа двигаться вверхс нижней половиной содержимого представления прокрутки.

Вместо этого оно начинается в правильном месте, но не перемещается с содержимым представления прокрутки.

AppDelegate.swift:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        window.contentViewController = ViewController(nibName: "View", bundle: nil)
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

ViewController.swift:

import Cocoa


class ViewController: NSViewController {
    @IBOutlet weak var view1: NSView!
    @IBOutlet weak var view2: NSView!

    override func viewDidLoad() {
        view1.colorBackground(NSColor.red)
        view2.colorBackground(NSColor.blue)
    }
}



extension NSView {

    func colorBackground(_ color: NSColor) {
        wantsLayer = true
        layer?.backgroundColor = color.cgColor
    }
}

View.xib:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <customObject id="-2" userLabel="File's Owner" customClass="ViewController" customModule="Test" customModuleProvider="target">
            <connections>
                <outlet property="view" destination="c22-O7-iKe" id="Yxc-sQ-AW4"/>
                <outlet property="view1" destination="8x7-dp-Hsw" id="evk-7F-37Z"/>
                <outlet property="view2" destination="f8D-v7-oc2" id="BgB-Fo-M7R"/>
            </connections>
        </customObject>
        <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
        <customObject id="-3" userLabel="Application" customClass="NSObject"/>
        <customView id="c22-O7-iKe">
            <rect key="frame" x="0.0" y="0.0" width="500" height="200"/>
            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
            <subviews>
                <scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="agq-bZ-fEr">
                    <rect key="frame" x="0.0" y="0.0" width="400" height="200"/>
                    <clipView key="contentView" id="iYq-AW-6CU">
                        <rect key="frame" x="1" y="1" width="398" height="198"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <view translatesAutoresizingMaskIntoConstraints="NO" id="Jr5-4g-mif">
                                <rect key="frame" x="-1" y="-201" width="400" height="400"/>
                                <subviews>
                                    <stackView distribution="fillEqually" orientation="vertical" alignment="leading" spacing="0.0" horizontalStackHuggingPriority="249.99998474121094" verticalStackHuggingPriority="249.99998474121094" detachesHiddenViews="YES" translatesAutoresizingMaskIntoConstraints="NO" id="SyR-ko-zr4">
                                        <rect key="frame" x="0.0" y="0.0" width="400" height="400"/>
                                        <subviews>
                                            <customView translatesAutoresizingMaskIntoConstraints="NO" id="8x7-dp-Hsw">
                                                <rect key="frame" x="0.0" y="96" width="400" height="304"/>
                                            </customView>
                                            <customView translatesAutoresizingMaskIntoConstraints="NO" id="f8D-v7-oc2">
                                                <rect key="frame" x="0.0" y="0.0" width="400" height="96"/>
                                            </customView>
                                        </subviews>
                                        <constraints>
                                            <constraint firstItem="f8D-v7-oc2" firstAttribute="width" secondItem="SyR-ko-zr4" secondAttribute="width" id="CUa-kB-FHn"/>
                                            <constraint firstItem="8x7-dp-Hsw" firstAttribute="width" secondItem="SyR-ko-zr4" secondAttribute="width" id="Qeh-ci-tD8"/>
                                        </constraints>
                                        <visibilityPriorities>
                                            <integer value="1000"/>
                                            <integer value="1000"/>
                                        </visibilityPriorities>
                                        <customSpacing>
                                            <real value="3.4028234663852886e+38"/>
                                            <real value="3.4028234663852886e+38"/>
                                        </customSpacing>
                                    </stackView>
                                </subviews>
                                <constraints>
                                    <constraint firstAttribute="trailing" secondItem="SyR-ko-zr4" secondAttribute="trailing" id="071-Ds-bqi"/>
                                    <constraint firstAttribute="bottom" secondItem="SyR-ko-zr4" secondAttribute="bottom" id="GBW-oP-mbV"/>
                                    <constraint firstAttribute="height" constant="400" id="LG2-HP-nAB"/>
                                    <constraint firstItem="SyR-ko-zr4" firstAttribute="leading" secondItem="Jr5-4g-mif" secondAttribute="leading" id="xno-LJ-JNv"/>
                                    <constraint firstItem="SyR-ko-zr4" firstAttribute="top" secondItem="Jr5-4g-mif" secondAttribute="top" id="y6g-52-NdQ"/>
                                </constraints>
                            </view>
                        </subviews>
                    </clipView>
                    <constraints>
                        <constraint firstItem="Jr5-4g-mif" firstAttribute="top" secondItem="agq-bZ-fEr" secondAttribute="top" id="Ln7-Pi-szE"/>
                        <constraint firstAttribute="trailing" secondItem="Jr5-4g-mif" secondAttribute="trailing" id="MO9-MN-kVt"/>
                        <constraint firstAttribute="height" constant="200" id="l0n-gW-nUT"/>
                        <constraint firstItem="Jr5-4g-mif" firstAttribute="leading" secondItem="agq-bZ-fEr" secondAttribute="leading" id="zbi-k8-oiU"/>
                    </constraints>
                    <scroller key="horizontalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="gZP-Yp-TOe">
                        <rect key="frame" x="1" y="183" width="398" height="16"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </scroller>
                    <scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="Vcv-Oy-xXi">
                        <rect key="frame" x="383" y="1" width="16" height="198"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </scroller>
                </scrollView>
                <box boxType="custom" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="wHO-ht-fwK">
                    <rect key="frame" x="450" y="-104" width="50" height="50"/>
                    <view key="contentView" id="7Ur-kr-AeI">
                        <rect key="frame" x="1" y="1" width="48" height="48"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                    </view>
                    <constraints>
                        <constraint firstAttribute="height" constant="50" id="Tof-Kf-eC0"/>
                        <constraint firstAttribute="width" constant="50" id="wqS-HE-kqB"/>
                    </constraints>
                    <color key="fillColor" red="1" green="0.3294117647" blue="0.3294117647" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                </box>
            </subviews>
            <constraints>
                <constraint firstItem="agq-bZ-fEr" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" id="CrV-le-r8T"/>
                <constraint firstAttribute="trailing" secondItem="wHO-ht-fwK" secondAttribute="trailing" id="Tas-pu-9sK"/>
                <constraint firstAttribute="trailing" secondItem="agq-bZ-fEr" secondAttribute="trailing" constant="100" id="VWL-ze-Lbx"/>
                <constraint firstAttribute="bottom" secondItem="agq-bZ-fEr" secondAttribute="bottom" id="gPO-Wu-Czj"/>
                <constraint firstItem="wHO-ht-fwK" firstAttribute="bottom" secondItem="f8D-v7-oc2" secondAttribute="top" id="qgy-WE-2Sh"/>
                <constraint firstItem="agq-bZ-fEr" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" id="y6u-QG-JtA"/>
            </constraints>
        </customView>
    </objects>
</document>
...