addArrangedSubview 是重叠视图

Posted

技术标签:

【中文标题】addArrangedSubview 是重叠视图【英文标题】:addArrangedSubview is overlapping views 【发布时间】:2020-01-29 19:54:14 【问题描述】:

我的问题 - 当我以编程方式将排列的子视图添加到堆栈视图时,它们只是堆积在右上角,如下所示。他们为什么不堆积起来?我尝试了很多很多东西,包括使用具有内在大小的视图。

stackview 被称为 mainStack 并且来自 xib。 mainStack 是一个垂直堆栈,其中 Alignment 设置为 Fill 并且 Distribution 设置为 Fill Equally (请参阅本问题底部的设置)。它包含一个蓝色背景的 UIView。为了这个问题,我使用 addArrangedSubviews 向 mainStack 添加了两个视图。这是我得到的:

这是我所期望的:

这是 xib 的代码:

class TaskSheet: UIView 

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var mainStack: UIStackView!


    override init(frame: CGRect) 
           super.init(frame: frame)
           setup()
       

       required init?(coder aDecoder: NSCoder) 
           super.init(coder: aDecoder)
           setup()
       

    func setup() 
        let nib = UINib(nibName: "TaskSheet", bundle: nil)
        nib.instantiate(withOwner: self, options: nil)
        contentView.frame = bounds
        addSubview(contentView)
    

这就是我尝试向 mainStack 添加视图的方式:

class PDFSheet: UIView 

    var taskSheet: TaskSheet!
    var sheetArray = [UIView]()

    func makeSheet() -> [UIView] 
        taskSheet = TaskSheet(frame: CGRect(x: 0, y: 0, width: 612, height: 792))

        let newView1 = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
        newView1.heightAnchor.constraint(equalToConstant: 128).isActive = true
        newView1.widthAnchor.constraint(equalToConstant: 240).isActive = true
        newView1.backgroundColor = .green

        let newView2 = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 64))
        newView2.heightAnchor.constraint(equalToConstant: 64).isActive = true
        newView2.widthAnchor.constraint(equalToConstant: 120).isActive = true
        newView2.backgroundColor = .yellow

        taskSheet.mainStack.addArrangedSubview(newView1)
        taskSheet.mainStack.addArrangedSubview(newView2)
        sheetArray.append(taskSheet)
        return sheetArray
    

还有,这里是显示 stackview 设置的 xib,以防万一……

【问题讨论】:

这有点令人困惑......你的 xib 的 TaskSheet 类显示了一个 @IBOutlet var contentView: UIView! - 但是你的代码正在调用 addSubview(contentView)?你打算有一个更复杂的xib吗?如果没有,您可以使用 TaskSheet 作为纯代码类而不是 xib,从而省去一些麻烦。 如果你使用约束,你应该将视图的translateAutoresizingMaskIntoConstraints属性设置为false 当你给视图提供不同的高度约束时,你不能让分布均匀填充,如果你告诉它们是不同的尺寸,它们将如何被均匀填充? 感谢@DonMag - 是的,实际的 xib 有很多。这只是精简版。我的问题的根源。 谢谢@javier rivarola - 是的,我试过 translatesAutoresizingMaskIntoContraints。 (虽然这不止一次是我问题的根源!)而且我知道如果视图大小不同,我会收到警告,但我想展示它们是如何重叠的,如果它们都是大小相同。如果一切正常,我相信 stackview 会简单地取代我的大小限制。 【参考方案1】:

我想我明白你的目的。

这是一个例子......

xib 布局(顶部标签、垂直堆栈视图、底部标签):

堆栈视图的属性:

TaskSheetPDFSheet 和示例视图控制器的代码:

class TaskSheet: UIView 

    @IBOutlet var contentView: UIView!
    @IBOutlet var mainStack: UIStackView!

    override init(frame: CGRect) 
        super.init(frame: frame)
        setup()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
        setup()
    

    func setup() 
        let nib = UINib(nibName: "TaskSheet", bundle: nil)
        nib.instantiate(withOwner: self, options: nil)
        addSubview(contentView)
        NSLayoutConstraint.activate([

            // constrain contentView on all 4 sides with 8-pts "padding"
            contentView.topAnchor.constraint(equalTo: topAnchor, constant: 8.0),
            contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8.0),
            contentView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8.0),
            contentView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8.0),

        ])
    


class PDFSheet: UIView 

    var taskSheet: TaskSheet!
    var sheetArray = [UIView]()

    override init(frame: CGRect) 
        super.init(frame: frame)
        _ = makeSheet()
    

    required init?(coder: NSCoder) 
        super.init(coder: coder)
        _ = makeSheet()
    

    func makeSheet() -> [UIView] 

        taskSheet = TaskSheet()

        let newView1 = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
        newView1.heightAnchor.constraint(equalToConstant: 128).isActive = true
        newView1.widthAnchor.constraint(equalToConstant: 240).isActive = true
        newView1.backgroundColor = .green

        let newView2 = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 64))
        newView2.heightAnchor.constraint(equalToConstant: 64).isActive = true
        newView2.widthAnchor.constraint(equalToConstant: 120).isActive = true
        newView2.backgroundColor = .yellow

        taskSheet.mainStack.addArrangedSubview(newView1)
        taskSheet.mainStack.addArrangedSubview(newView2)
        sheetArray.append(taskSheet)

        addSubview(taskSheet)

        taskSheet.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([

            // constrain taskSheet on all 4 sides
            taskSheet.topAnchor.constraint(equalTo: topAnchor, constant: 8.0),
            taskSheet.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8.0),
            taskSheet.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8.0),
            taskSheet.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8.0),

        ])

        return sheetArray
    


class TaskViewController: UIViewController 

    var theSheetView: PDFSheet!

    override func viewDidLoad() 
        super.viewDidLoad()

        theSheetView = PDFSheet()
        theSheetView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(theSheetView)

        let g = view.safeAreaLayoutGuide

        NSLayoutConstraint.activate([

            // constrain the sheet view on all top, leading, trailing with 32-pts "padding"
            theSheetView.topAnchor.constraint(equalTo: g.topAnchor, constant: 32.0),
            theSheetView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 32.0),
            theSheetView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -32.0),
            // NO height or bottom constraint
        ])

    


这里是 xib 文件的源代码(以便于检查)编辑:哎呀,粘贴了错误的 xml 源代码——现在已修复

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15505" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TaskSheet" customModule="scratchy" customModuleProvider="target">
            <connections>
                <outlet property="contentView" destination="TFh-sZ-4cx" id="zaP-M3-nAu"/>
                <outlet property="mainStack" destination="oGz-Bu-nCT" id="oCb-IB-Q4i"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="iN0-l3-epB">
            <rect key="frame" x="0.0" y="0.0"  />
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TFh-sZ-4cx">
                    <rect key="frame" x="8" y="8"  />
                    <subviews>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Top Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jZ3-yl-TaR">
                            <rect key="frame" x="0.0" y="0.0"  />
                            <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                        <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="oGz-Bu-nCT">
                            <rect key="frame" x="0.0" y="21"  />
                        </stackView>
                        <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Bottom Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8bg-zV-k8Q">
                            <rect key="frame" x="0.0" y="278"  />
                            <color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                            <nil key="textColor"/>
                            <nil key="highlightedColor"/>
                        </label>
                    </subviews>
                    <color key="backgroundColor" red="0.36312681436538696" green="0.3205370306968689" blue="0.87124341726303101" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <constraints>
                        <constraint firstItem="oGz-Bu-nCT" firstAttribute="top" secondItem="jZ3-yl-TaR" secondAttribute="bottom" id="3FB-p9-cGU"/>
                        <constraint firstItem="8bg-zV-k8Q" firstAttribute="leading" secondItem="TFh-sZ-4cx" secondAttribute="leading" id="7bO-hv-chQ"/>
                        <constraint firstItem="oGz-Bu-nCT" firstAttribute="leading" secondItem="TFh-sZ-4cx" secondAttribute="leading" id="G5h-mz-ag5"/>
                        <constraint firstItem="jZ3-yl-TaR" firstAttribute="top" secondItem="TFh-sZ-4cx" secondAttribute="top" id="T1H-hj-4jJ"/>
                        <constraint firstAttribute="bottom" secondItem="8bg-zV-k8Q" secondAttribute="bottom" id="TYr-rY-NAc"/>
                        <constraint firstAttribute="trailing" secondItem="oGz-Bu-nCT" secondAttribute="trailing" id="VA9-gN-L1a"/>
                        <constraint firstAttribute="trailing" secondItem="8bg-zV-k8Q" secondAttribute="trailing" id="Vv5-P9-EGo"/>
                        <constraint firstItem="jZ3-yl-TaR" firstAttribute="leading" secondItem="TFh-sZ-4cx" secondAttribute="leading" id="XZc-QB-dm1"/>
                        <constraint firstItem="8bg-zV-k8Q" firstAttribute="top" secondItem="oGz-Bu-nCT" secondAttribute="bottom" id="ayn-E8-jo9"/>
                        <constraint firstAttribute="trailing" secondItem="jZ3-yl-TaR" secondAttribute="trailing" id="v4D-bJ-ltC"/>
                    </constraints>
                </view>
            </subviews>
            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            <constraints>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="TFh-sZ-4cx" secondAttribute="trailing" constant="8" id="cgO-BT-ruo"/>
                <constraint firstItem="TFh-sZ-4cx" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="8" id="rAA-Vf-F0B"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" secondItem="TFh-sZ-4cx" secondAttribute="bottom" constant="8" id="rhR-sH-KEq"/>
                <constraint firstItem="TFh-sZ-4cx" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" constant="8" id="sag-F8-NuC"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <point key="canvasLocation" x="148" y="49.925037481259373"/>
        </view>
    </objects>
</document>

结果:


EDIt 稍微修改代码以生成 OP 添加的“预期结果”图像:

删除了标签(我将它们作为附加元素的示例) 将 stackView 的所有 4 面都限制为 0 将 stackView 更改为 Aligment: FillDistribution: Fill Equally

TaskSheet.xib

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TaskSheet" customModule="scratchy" customModuleProvider="target">
            <connections>
                <outlet property="contentView" destination="TFh-sZ-4cx" id="zaP-M3-nAu"/>
                <outlet property="mainStack" destination="oGz-Bu-nCT" id="oCb-IB-Q4i"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="iN0-l3-epB">
            <rect key="frame" x="0.0" y="0.0"  />
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="TFh-sZ-4cx">
                    <rect key="frame" x="8" y="8"  />
                    <subviews>
                        <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="oGz-Bu-nCT">
                            <rect key="frame" x="0.0" y="0.0"  />
                        </stackView>
                    </subviews>
                    <color key="backgroundColor" red="0.36312681436538696" green="0.3205370306968689" blue="0.87124341726303101" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <constraints>
                        <constraint firstItem="oGz-Bu-nCT" firstAttribute="leading" secondItem="TFh-sZ-4cx" secondAttribute="leading" id="G5h-mz-ag5"/>
                        <constraint firstAttribute="bottom" secondItem="oGz-Bu-nCT" secondAttribute="bottom" id="SIv-DX-ZpP"/>
                        <constraint firstAttribute="trailing" secondItem="oGz-Bu-nCT" secondAttribute="trailing" id="VA9-gN-L1a"/>
                        <constraint firstItem="oGz-Bu-nCT" firstAttribute="top" secondItem="TFh-sZ-4cx" secondAttribute="top" id="hPW-P3-dsk"/>
                    </constraints>
                </view>
            </subviews>
            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            <constraints>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="TFh-sZ-4cx" secondAttribute="trailing" constant="8" id="cgO-BT-ruo"/>
                <constraint firstItem="TFh-sZ-4cx" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="8" id="rAA-Vf-F0B"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" secondItem="TFh-sZ-4cx" secondAttribute="bottom" constant="8" id="rhR-sH-KEq"/>
                <constraint firstItem="TFh-sZ-4cx" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" constant="8" id="sag-F8-NuC"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <point key="canvasLocation" x="148" y="49.925037481259373"/>
        </view>
    </objects>
</document>

class TaskSheet: UIView 

    @IBOutlet var contentView: UIView!
    @IBOutlet var mainStack: UIStackView!

    override init(frame: CGRect) 
        super.init(frame: frame)
        setup()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
        setup()
    

    func setup() 
        let nib = UINib(nibName: "TaskSheet", bundle: nil)
        nib.instantiate(withOwner: self, options: nil)
        addSubview(contentView)
        NSLayoutConstraint.activate([

            // constrain contentView on all 4 sides with 0-pts "padding"
            contentView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0),
            contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0),
            contentView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
            contentView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),

        ])

    


class PDFSheet: UIView 

    var taskSheet: TaskSheet!
    var sheetArray = [UIView]()

    override init(frame: CGRect) 
        super.init(frame: frame)
        _ = makeSheet()
    

    required init?(coder: NSCoder) 
        super.init(coder: coder)
        _ = makeSheet()
    

    func makeSheet() -> [UIView] 

        taskSheet = TaskSheet()

        let newView1 = UIView()
        newView1.backgroundColor = .green

        let newView2 = UIView()
        newView2.backgroundColor = .yellow

        let spacerView = UIView()
        spacerView.backgroundColor = .clear

        // to get the "expected result" as shown in the OP's image,
        //  a 3-part stack view with equal heights,
        //  an easy way is to add a clear "spacer view" as the
        //  first - "top" - arranged subview

        taskSheet.mainStack.addArrangedSubview(spacerView)
        taskSheet.mainStack.addArrangedSubview(newView1)
        taskSheet.mainStack.addArrangedSubview(newView2)
        sheetArray.append(taskSheet)

        addSubview(taskSheet)

        taskSheet.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([

            // constrain taskSheet on all 4 sides
            taskSheet.topAnchor.constraint(equalTo: topAnchor, constant: 0.0),
            taskSheet.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0),
            taskSheet.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
            taskSheet.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),

        ])

        return sheetArray
    


class TaskViewController: UIViewController 

    var theSheetView: PDFSheet!

    override func viewDidLoad() 
        super.viewDidLoad()

        theSheetView = PDFSheet()
        theSheetView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(theSheetView)

        let g = view.safeAreaLayoutGuide

        NSLayoutConstraint.activate([

            // constrain the sheet view on top and leading at 40-pts (just so it's not flush with top/left of the view)
            // with specified width: 612 and height 792
            theSheetView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
            theSheetView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
            theSheetView.widthAnchor.constraint(equalToConstant: 612),
            theSheetView.heightAnchor.constraint(equalToConstant: 792),
        ])

    


在第三代 iPad Air 上运行的结果(以匹配 OP 的 width: 612, height: 792 规范):


另一个编辑:

这可能更接近 OP 的意图。

PDFSheet 类现在被视为“视图提供者”而不是视图本身。 返回的TaskSheet 视图数组的第一个元素(当前仅包含一个视图)将作为子视图添加到 viewController 的视图中。

与上述相同的 .xib 文件。

class TaskSheet: UIView 

    @IBOutlet var contentView: UIView!
    @IBOutlet var mainStack: UIStackView!

    override init(frame: CGRect) 
        super.init(frame: frame)
        setup()
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
        setup()
    

    func setup() 
        let nib = UINib(nibName: "TaskSheet", bundle: nil)
        nib.instantiate(withOwner: self, options: nil)
        addSubview(contentView)

        NSLayoutConstraint.activate([

            // constrain contentView on all 4 sides with 0-pts "padding"
            contentView.topAnchor.constraint(equalTo: topAnchor, constant: 0.0),
            contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0.0),
            contentView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0),
            contentView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0.0),

        ])

    


class PDFSheet: UIView 

    var taskSheet: TaskSheet!
    var sheetArray = [UIView]()

    func makeSheet() -> [UIView] 

        taskSheet = TaskSheet(frame: CGRect(x: 0, y: 0, width: 612, height: 792))

        let newView1 = UIView()
        newView1.backgroundColor = .green

        let newView2 = UIView()
        newView2.backgroundColor = .yellow

        let spacerView = UIView()
        spacerView.backgroundColor = .clear

        // to get the "expected result" as shown in the OP's image,
        //  a 3-part stack view with equal heights,
        //  an easy way is to add a clear "spacer view" as the
        //  first - "top" - arranged subview

        taskSheet.mainStack.addArrangedSubview(spacerView)
        taskSheet.mainStack.addArrangedSubview(newView1)
        taskSheet.mainStack.addArrangedSubview(newView2)

        sheetArray.append(taskSheet)

        return sheetArray
    


class TaskViewController: UIViewController 

    var theSheetView: PDFSheet!

    override func viewDidLoad() 
        super.viewDidLoad()

        theSheetView = PDFSheet()

        let views: [UIView] = theSheetView.makeSheet()

        guard let v = views.first else 
            fatalError("PDFSheet failed to create TaskSheet")
        

    // note: At this point, the view has not been added to the
    // view hierarchy. If you're going to do something with it,
    // such as output it to a png or pdf, for example, you need
    // to tell auto-layout to do its work
    v.setNeedsLayout()
    v.layoutIfNeeded()

    let s = v.exportAsPdfFromView()
        view.addSubview(v)

    


和(视觉上)相同的结果。这一次,生成的 TaskSheet 视图只是被添加到 viewController 的视图中,没有任何偏移:

【讨论】:

您的回答与问题所要求的预期结果不符。 @javierrivarola - 我的答案发布在 OP 编辑​​他的问题以澄清他的“预期结果”(值得检查时间戳)。 没关系,他的问题是stackView表现出意外的行为,你的回答也在做,预期的行为是stackView的行为之一,所以你不需要等他这么说,很明显。 @javierrivarola - 好的,我有点困惑......我的答案中的 stackView 如何以意想不到的方式表现? OP 的代码在排列的子视图上设置宽度和高度——因此 预期结果 将是不同大小的子视图。在我的示例中,黄色子视图 排列在绿色子视图下方,就像人们对 stackView 所期望的一样。 @javierrivarola - 根据 OP 的代码,绿色视图是 128 x 240,黄色视图是 64 x 120 - 它们是 NOT 相互重叠。运行我的示例代码,如果您需要自己确认,请使用Debug View Hierarchy

以上是关于addArrangedSubview 是重叠视图的主要内容,如果未能解决你的问题,请参考以下文章

addArrangedSubview 没有动画

iOS - UIStackView - zorder

我是不是应该将“addArrangedSubview”附在动画块中?

addSubview 和 addArrangedSubview 不显示内容

Swift stackview在中心添加子视图

如何防止视图重叠?