当运行应用程序时,堆栈视图中的项目将不在堆栈中。

问题描述 投票:0回答:1

我是第一次使用堆栈视图,我开始了解它们。

我附上一张图片,说明我如何在堆栈视图中分布我的元素。enter image description here

到目前为止,一切都很好,但当我运行应用程序时,它在左上角容纳了图像。

enter image description hereenter image description here

我做错了什么?我的约束没有给我标记错误

附件捕获制约因素enter image description hereenter image description here

ios swift xcode constraints uistackview
1个回答
0
投票

从你发布的图片来看,它 样子 就像你正确设置了约束条件一样。

一个注意点:当你的UI元素没有连接到 @IBOutlet. 检查约束条件,看到的是这样的东西。

View.width = View.width

与(如果我没看错的话)你的约束条件相比,可能会相当混乱。

OrangeView.width = GreenView.width

我试着复制你的布局,我没有遇到你遇到的问题。试试这个......如果 Imagen EspañolImagen Ingles 视图停留在它们应该停留的地方,将我的约束条件与你的约束条件进行比较,看看你是否发现任何导致问题的差异。

这是Storyboard的源码。

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="DYH-hb-0xS">
    <device id="retina6_1" orientation="portrait" appearance="light"/>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Select Language View Controller-->
        <scene sceneID="3Vw-gp-IQV">
            <objects>
                <viewController id="DYH-hb-0xS" customClass="SelectLanguageViewController" customModule="MiniScratch" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="W6i-IC-bIv">
                        <rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="pencil.circle" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="ugX-ma-vgM">
                                <rect key="frame" x="150" y="180.5" width="114" height="113"/>
                                <constraints>
                                    <constraint firstAttribute="width" constant="100" id="TOW-nR-HFA"/>
                                    <constraint firstAttribute="width" secondItem="ugX-ma-vgM" secondAttribute="height" multiplier="1:1" id="dqp-Bi-aaq"/>
                                    <constraint firstAttribute="width" secondItem="ugX-ma-vgM" secondAttribute="height" multiplier="1:1" id="pRb-Mg-k7c"/>
                                    <constraint firstAttribute="width" constant="114" id="uBv-y0-pAP"/>
                                </constraints>
                                <variation key="default">
                                    <mask key="constraints">
                                        <exclude reference="TOW-nR-HFA"/>
                                        <exclude reference="dqp-Bi-aaq"/>
                                    </mask>
                                </variation>
                            </imageView>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Le Blanc" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dFl-iU-tdo">
                                <rect key="frame" x="151.5" y="302" width="111" height="33.5"/>
                                <fontDescription key="fontDescription" type="boldSystem" pointSize="28"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Please select a language" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BOc-3S-ImB">
                                <rect key="frame" x="102" y="343.5" width="210.5" height="23"/>
                                <fontDescription key="fontDescription" type="system" pointSize="19"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <stackView opaque="NO" contentMode="scaleToFill" spacing="29" translatesAutoresizingMaskIntoConstraints="NO" id="tQ1-jP-jlt" userLabel="OrangeGreenStack">
                                <rect key="frame" x="92.5" y="386.5" width="229" height="150"/>
                                <subviews>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="LhZ-T7-3jj" userLabel="GreenView">
                                        <rect key="frame" x="0.0" y="0.0" width="100" height="150"/>
                                        <subviews>
                                            <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="mfE-ec-mp7" userLabel="SpanishStack">
                                                <rect key="frame" x="10" y="10" width="80" height="130"/>
                                                <subviews>
                                                    <imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="pencil.circle" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="hDJ-VM-0H8">
                                                        <rect key="frame" x="0.0" y="0.5" width="80" height="60"/>
                                                    </imageView>
                                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Spanish" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="106-nV-uFU">
                                                        <rect key="frame" x="0.0" y="69" width="80" height="61"/>
                                                        <fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
                                                        <nil key="textColor"/>
                                                        <nil key="highlightedColor"/>
                                                    </label>
                                                </subviews>
                                            </stackView>
                                        </subviews>
                                        <color key="backgroundColor" red="0.2048633993" green="0.78171128030000003" blue="0.34992033239999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstAttribute="bottom" secondItem="mfE-ec-mp7" secondAttribute="bottom" constant="10" id="Ap9-Re-UGN"/>
                                            <constraint firstAttribute="trailing" secondItem="mfE-ec-mp7" secondAttribute="trailing" constant="10" id="D8V-cT-vkM"/>
                                            <constraint firstAttribute="height" constant="150" id="MUG-b3-8IY"/>
                                            <constraint firstAttribute="width" constant="100" id="NK3-gz-vqg"/>
                                            <constraint firstItem="mfE-ec-mp7" firstAttribute="top" secondItem="LhZ-T7-3jj" secondAttribute="top" constant="10" id="RXb-2e-fcv"/>
                                            <constraint firstItem="mfE-ec-mp7" firstAttribute="leading" secondItem="LhZ-T7-3jj" secondAttribute="leading" constant="10" id="x7P-dZ-XU3"/>
                                        </constraints>
                                    </view>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Oay-YI-rrw" userLabel="OrangeView">
                                        <rect key="frame" x="129" y="0.0" width="100" height="150"/>
                                        <subviews>
                                            <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="Fdg-fY-ViJ" userLabel="EnglishStack">
                                                <rect key="frame" x="10" y="10" width="80" height="130"/>
                                                <subviews>
                                                    <imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="pencil.circle" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="nTD-5F-pvb">
                                                        <rect key="frame" x="0.0" y="0.5" width="80" height="60"/>
                                                    </imageView>
                                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="English" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iVa-qQ-BKQ">
                                                        <rect key="frame" x="0.0" y="69" width="80" height="61"/>
                                                        <fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
                                                        <nil key="textColor"/>
                                                        <nil key="highlightedColor"/>
                                                    </label>
                                                </subviews>
                                            </stackView>
                                        </subviews>
                                        <color key="backgroundColor" red="0.9999875426" green="0.6251454353" blue="0.0002199066948" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstAttribute="trailing" secondItem="Fdg-fY-ViJ" secondAttribute="trailing" constant="10" id="OFt-Ds-7ug"/>
                                            <constraint firstItem="Fdg-fY-ViJ" firstAttribute="top" secondItem="Oay-YI-rrw" secondAttribute="top" constant="10" id="afy-NB-rae"/>
                                            <constraint firstItem="Fdg-fY-ViJ" firstAttribute="leading" secondItem="Oay-YI-rrw" secondAttribute="leading" constant="10" id="egU-Sd-RlC"/>
                                            <constraint firstAttribute="height" constant="150" id="xVk-EX-tZW"/>
                                            <constraint firstAttribute="bottom" secondItem="Fdg-fY-ViJ" secondAttribute="bottom" constant="10" id="yVL-mM-9jg"/>
                                        </constraints>
                                    </view>
                                </subviews>
                                <constraints>
                                    <constraint firstItem="Oay-YI-rrw" firstAttribute="width" secondItem="LhZ-T7-3jj" secondAttribute="width" id="Cow-kv-pFC"/>
                                </constraints>
                            </stackView>
                        </subviews>
                        <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
                        <constraints>
                            <constraint firstItem="ugX-ma-vgM" firstAttribute="top" secondItem="AjE-1X-LS6" secondAttribute="top" constant="136" id="KLb-5n-W3U"/>
                            <constraint firstItem="dFl-iU-tdo" firstAttribute="top" secondItem="ugX-ma-vgM" secondAttribute="bottom" constant="8" id="Oub-GU-Clu"/>
                            <constraint firstItem="tQ1-jP-jlt" firstAttribute="top" secondItem="BOc-3S-ImB" secondAttribute="bottom" constant="20" id="TCb-3H-BSD"/>
                            <constraint firstItem="ugX-ma-vgM" firstAttribute="centerX" secondItem="W6i-IC-bIv" secondAttribute="centerX" id="VCy-1R-a2z"/>
                            <constraint firstItem="ugX-ma-vgM" firstAttribute="centerX" secondItem="W6i-IC-bIv" secondAttribute="centerX" id="aus-IV-w9l"/>
                            <constraint firstItem="BOc-3S-ImB" firstAttribute="top" secondItem="dFl-iU-tdo" secondAttribute="bottom" constant="8" id="mBb-7f-Tv2"/>
                            <constraint firstItem="dFl-iU-tdo" firstAttribute="centerX" secondItem="W6i-IC-bIv" secondAttribute="centerX" id="re8-ok-hGZ"/>
                            <constraint firstItem="tQ1-jP-jlt" firstAttribute="centerX" secondItem="W6i-IC-bIv" secondAttribute="centerX" id="xes-5F-gJB"/>
                            <constraint firstItem="BOc-3S-ImB" firstAttribute="centerX" secondItem="W6i-IC-bIv" secondAttribute="centerX" id="xtm-sW-5Q9"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="AjE-1X-LS6"/>
                        <variation key="default">
                            <mask key="constraints">
                                <exclude reference="VCy-1R-a2z"/>
                            </mask>
                        </variation>
                    </view>
                    <connections>
                        <outlet property="encabezadoEtiqueta" destination="BOc-3S-ImB" id="0vm-Zq-Ibk"/>
                        <outlet property="etiquetaEspañol" destination="106-nV-uFU" id="2pC-b9-5vQ"/>
                        <outlet property="etiquetaIngles" destination="iVa-qQ-BKQ" id="fRg-Ii-ZOt"/>
                        <outlet property="imagenEspañol" destination="hDJ-VM-0H8" id="uhg-7L-BV2"/>
                        <outlet property="imagenIngles" destination="nTD-5F-pvb" id="isY-SQ-Rjy"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="KGv-sc-F94" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-504.375" y="1450"/>
        </scene>
    </scenes>
    <resources>
        <image name="pencil.circle" catalog="system" width="128" height="121"/>
    </resources>
</document>

而这里是带有 @IBOutlet 连接。

//
//  SelectLanguageViewController.swift
//  Created by Don Mag on 4/24/20.
//

import UIKit

class SelectLanguageViewController: UIViewController {

    @IBOutlet var encabezadoEtiqueta: UILabel!
    @IBOutlet var imagenEspañol: UIImageView!
    @IBOutlet var imagenIngles: UIImageView!
    @IBOutlet var etiquetaEspañol: UILabel!
    @IBOutlet var etiquetaIngles: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.