故事板混乱:UITextView 和 UIView 不可见,UITextFields 不可编辑?
Posted
技术标签:
【中文标题】故事板混乱:UITextView 和 UIView 不可见,UITextFields 不可编辑?【英文标题】:Storyboard confusion: UITextView and UIView not visible, UITextFields not editable? 【发布时间】:2018-06-04 01:37:42 【问题描述】:我觉得我忽略了一些基本的东西。我已经列出了UIViewController
,如下所示。
我在 Interface Builder 中没有收到任何错误或警告,但是当我在模拟器中运行应用程序时,UITextView 和红色 UIView
没有出现。 (另外,UITextFields
不可编辑。)
我在viewDidLoad:
的末尾添加了几个NSLog
,然后看到UITextView
和UIView
是我期望的位置:
2018-06-03 18:09:30.304537-0700 TestMe[37059:1239027] myTextView = 48.5, 0, 278, 60
2018-06-03 18:09:30.304876-0700 TestMe[37059:1239027] facebookView = 52.5, 94.5, 270, 80
它们根本不可见。为什么?
这是 main.storyboard:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="Stack View standard spacing" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="TestMe" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" alignment="center" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="efN-Uu-EM8">
<rect key="frame" x="0.0" y="40" />
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="Why does this text field disappear and the view below it? I'm confused... very confused..." textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="uN2-2x-62N">
<rect key="frame" x="48.5" y="0.0" />
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="60" id="eD0-mv-hDl"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="cQP-PE-x9G" userLabel="Facebook View">
<rect key="frame" x="52.5" y="94.5" />
<color key="backgroundColor" red="1" green="0.1098039225" blue="0.14901961389999999" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="80" id="mff-q6-fMf"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8I8-cQ-10b" userLabel="Separator View">
<rect key="frame" x="0.0" y="209" />
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="t6G-KG-jL8" userLabel="Separator">
<rect key="frame" x="19" y="9" />
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="2" id="zlr-In-RmU"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" or " textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="M6X-pf-iz5">
<rect key="frame" x="175" y="0.0" />
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="2Nu-OR-NmV"/>
<constraint firstItem="M6X-pf-iz5" firstAttribute="centerX" secondItem="8I8-cQ-10b" secondAttribute="centerX" id="VOL-QV-LjO"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="centerY" secondItem="8I8-cQ-10b" secondAttribute="centerY" id="YK8-Pj-sW5"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="centerX" secondItem="8I8-cQ-10b" secondAttribute="centerX" id="w7y-2K-QxP"/>
<constraint firstItem="M6X-pf-iz5" firstAttribute="centerY" secondItem="8I8-cQ-10b" secondAttribute="centerY" id="yyh-r9-Dmx"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Odz-UD-YHc">
<rect key="frame" x="67.5" y="263.5" />
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="A2n-PC-13u" userLabel="Login View">
<rect key="frame" x="20" y="84" />
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" alignment="center" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="TMi-iY-XF2">
<rect key="frame" x="0.0" y="0.0" />
<subviews>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="email" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="sq9-kJ-uel">
<rect key="frame" x="0.0" y="0.0" />
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="200" id="DDC-lb-W1o"/>
</constraints>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="email"/>
</textField>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="new password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="jaX-Ys-zsJ">
<rect key="frame" x="0.0" y="38" />
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="password"/>
</textField>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="retype password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Iki-yA-kWn">
<rect key="frame" x="0.0" y="76" />
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="password"/>
</textField>
</subviews>
<constraints>
<constraint firstItem="Iki-yA-kWn" firstAttribute="width" secondItem="sq9-kJ-uel" secondAttribute="width" id="TV0-Fc-Zy6"/>
<constraint firstItem="jaX-Ys-zsJ" firstAttribute="width" secondItem="sq9-kJ-uel" secondAttribute="width" id="ueB-9T-rcU"/>
</constraints>
</stackView>
<button opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="252" verticalCompressionResistancePriority="748" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bkQ-lZ-TGN">
<rect key="frame" x="0.0" y="126" />
<color key="backgroundColor" red="0.89275385269999996" green="0.94806676079999996" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="Create account"/>
</button>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="A2n-PC-13u" firstAttribute="centerX" secondItem="Odz-UD-YHc" secondAttribute="centerX" id="ECO-de-jMM"/>
<constraint firstItem="A2n-PC-13u" firstAttribute="centerY" secondItem="Odz-UD-YHc" secondAttribute="centerY" id="uBG-6V-L4T"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="8I8-cQ-10b" secondAttribute="trailing" id="LOw-Vq-fCr"/>
<constraint firstItem="8I8-cQ-10b" firstAttribute="leading" secondItem="efN-Uu-EM8" secondAttribute="leading" id="dxM-bE-lPs"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Odz-UD-YHc" firstAttribute="height" secondItem="6Tk-OE-BBY" secondAttribute="height" multiplier="0.5" id="1Cl-gZ-RGu"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="efN-Uu-EM8" secondAttribute="bottom" constant="40" id="2f9-c8-EJL"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="efN-Uu-EM8" secondAttribute="trailing" id="7qG-T5-VCR"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="width" secondItem="6Tk-OE-BBY" secondAttribute="width" multiplier="0.9" id="9M3-cG-Ldp"/>
<constraint firstItem="efN-Uu-EM8" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" constant="40" id="PaP-tg-Ne3"/>
<constraint firstItem="efN-Uu-EM8" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="VXh-b8-3ay"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="facebookView" destination="cQP-PE-x9G" id="LXX-lM-1tn"/>
<outlet property="myTextView" destination="uN2-2x-62N" id="coF-vZ-FrJ"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
我正在运行 Xcode 9.4 (9F1027a)。非常感谢任何帮助。
【问题讨论】:
您是否添加了 textview 的 top 、 left 和 right 约束? 运行应用程序并使用视图调试器找出“丢失”视图的位置。 你只提供了高度并没有将它们固定到视图 【参考方案1】:您没有在每个视图上适当地设置 top-bottom-leading-trailing-width-height 约束,因此编译中的放置错误并且文本字段也因此不可编辑。
看看你我编辑的代码可能更容易理解
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="login" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" alignment="center" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="efN-Uu-EM8">
<rect key="frame" x="0.0" y="40" />
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="Why does this text field disappear and the view below it? I'm confused... very confused..." textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="uN2-2x-62N">
<rect key="frame" x="48.5" y="0.0" />
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="60" id="eD0-mv-hDl"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="cQP-PE-x9G" userLabel="Facebook View">
<rect key="frame" x="52.5" y="107" />
<color key="backgroundColor" red="1" green="0.1098039225" blue="0.14901961389999999" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="80" id="mff-q6-fMf"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8I8-cQ-10b" userLabel="Separator View">
<rect key="frame" x="0.0" y="234" />
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="t6G-KG-jL8" userLabel="Separator">
<rect key="frame" x="19" y="9" />
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="2" id="zlr-In-RmU"/>
</constraints>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" or " textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="M6X-pf-iz5">
<rect key="frame" x="175" y="0.5" />
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="2Nu-OR-NmV"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="leading" secondItem="8I8-cQ-10b" secondAttribute="leading" constant="19" id="KmU-LN-V4W"/>
<constraint firstItem="M6X-pf-iz5" firstAttribute="centerX" secondItem="8I8-cQ-10b" secondAttribute="centerX" id="VOL-QV-LjO"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="centerY" secondItem="8I8-cQ-10b" secondAttribute="centerY" id="YK8-Pj-sW5"/>
<constraint firstItem="t6G-KG-jL8" firstAttribute="centerX" secondItem="8I8-cQ-10b" secondAttribute="centerX" id="w7y-2K-QxP"/>
<constraint firstItem="M6X-pf-iz5" firstAttribute="centerY" secondItem="8I8-cQ-10b" secondAttribute="centerY" id="yyh-r9-Dmx"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Odz-UD-YHc">
<rect key="frame" x="67.5" y="301" />
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" spacing="20" translatesAutoresizingMaskIntoConstraints="NO" id="A2n-PC-13u" userLabel="Login View">
<rect key="frame" x="20" y="65.5" />
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="equalSpacing" alignment="center" spacingType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="TMi-iY-XF2">
<rect key="frame" x="0.0" y="0.0" />
<subviews>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="email" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="sq9-kJ-uel">
<rect key="frame" x="0.0" y="0.0" />
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="200" id="DDC-lb-W1o"/>
</constraints>
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="email"/>
</textField>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="new password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="jaX-Ys-zsJ">
<rect key="frame" x="0.0" y="38" />
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="password"/>
</textField>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="retype password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Iki-yA-kWn">
<rect key="frame" x="0.0" y="76" />
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" textContentType="password"/>
</textField>
</subviews>
<constraints>
<constraint firstItem="Iki-yA-kWn" firstAttribute="width" secondItem="sq9-kJ-uel" secondAttribute="width" id="TV0-Fc-Zy6"/>
<constraint firstItem="jaX-Ys-zsJ" firstAttribute="width" secondItem="sq9-kJ-uel" secondAttribute="width" id="ueB-9T-rcU"/>
</constraints>
</stackView>
<button opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="252" verticalCompressionResistancePriority="748" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="bkQ-lZ-TGN">
<rect key="frame" x="0.0" y="126" />
<color key="backgroundColor" red="0.89275385269999996" green="0.94806676079999996" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<state key="normal" title="Create account"/>
</button>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="A2n-PC-13u" firstAttribute="centerX" secondItem="Odz-UD-YHc" secondAttribute="centerX" id="ECO-de-jMM"/>
<constraint firstAttribute="height" constant="286" id="rbx-xl-vgb"/>
<constraint firstAttribute="width" constant="240" id="spR-bu-Zx0"/>
<constraint firstItem="A2n-PC-13u" firstAttribute="centerY" secondItem="Odz-UD-YHc" secondAttribute="centerY" id="uBG-6V-L4T"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstItem="8I8-cQ-10b" firstAttribute="top" secondItem="cQP-PE-x9G" secondAttribute="bottom" constant="47" id="6eh-Ds-57M"/>
<constraint firstAttribute="trailing" secondItem="uN2-2x-62N" secondAttribute="trailing" constant="48.5" id="7rd-h4-aIp"/>
<constraint firstItem="cQP-PE-x9G" firstAttribute="top" secondItem="uN2-2x-62N" secondAttribute="bottom" constant="47" id="8gW-mV-6tW"/>
<constraint firstAttribute="trailing" secondItem="8I8-cQ-10b" secondAttribute="trailing" id="LOw-Vq-fCr"/>
<constraint firstItem="uN2-2x-62N" firstAttribute="leading" secondItem="efN-Uu-EM8" secondAttribute="leading" constant="48.5" id="Ot8-hl-xJo"/>
<constraint firstItem="8I8-cQ-10b" firstAttribute="centerY" secondItem="efN-Uu-EM8" secondAttribute="centerY" constant="-50" id="P1h-Js-zjz"/>
<constraint firstAttribute="bottom" secondItem="Odz-UD-YHc" secondAttribute="bottom" id="QT0-FI-0JI"/>
<constraint firstItem="Odz-UD-YHc" firstAttribute="top" secondItem="8I8-cQ-10b" secondAttribute="bottom" constant="47" id="SsW-FE-H6c"/>
<constraint firstItem="cQP-PE-x9G" firstAttribute="leading" secondItem="efN-Uu-EM8" secondAttribute="leading" constant="52.5" id="coV-Rh-6jK"/>
<constraint firstItem="8I8-cQ-10b" firstAttribute="leading" secondItem="efN-Uu-EM8" secondAttribute="leading" id="dxM-bE-lPs"/>
<constraint firstAttribute="trailing" secondItem="cQP-PE-x9G" secondAttribute="trailing" constant="52.5" id="fIf-qS-rMG"/>
<constraint firstItem="uN2-2x-62N" firstAttribute="top" secondItem="efN-Uu-EM8" secondAttribute="top" id="nYZ-5b-P9P"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="efN-Uu-EM8" secondAttribute="bottom" constant="40" id="2f9-c8-EJL"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="efN-Uu-EM8" secondAttribute="trailing" id="7qG-T5-VCR"/>
<constraint firstItem="efN-Uu-EM8" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" constant="40" id="PaP-tg-Ne3"/>
<constraint firstItem="efN-Uu-EM8" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="VXh-b8-3ay"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
希望对您有所帮助。
【讨论】:
我无法使用您的 Storyboard 代码@Fadielse,但通过采纳您的建议并添加更多约束,我能够使其正常工作。我仍然感到困惑的是,当我在 viewDidLoad 中打印出红色 UIView 的框架时,它似乎放置正确且大小合适。所以我相信我会再次遇到这个问题。但这是进步!【参考方案2】:在创建了一个与您的视图层次结构相似的示例后,我意识到由于您的文本视图和红色背景的视图缺少宽度约束,因此它们在运行时的宽度为零。尝试将它们的宽度赋予与父堆栈视图相同的宽度,它们将被渲染。
此外,在使用垂直堆栈视图时,对子视图进行高度约束并不是一个好主意,因为堆栈视图本身会为渲染其子视图创建一些约束,这可能会导致运行时的约束冲突。请参阅此link 了解更多信息。
【讨论】:
以上是关于故事板混乱:UITextView 和 UIView 不可见,UITextFields 不可编辑?的主要内容,如果未能解决你的问题,请参考以下文章
使用故事板、自动布局和 Swift 的多行 UILabel 和 UITextView