Ios ChildViewController的高度和宽度';的视图不同于其父视图控制器';s观点

Ios ChildViewController的高度和宽度';的视图不同于其父视图控制器';s观点,ios,swift,Ios,Swift,我已将ChildViewController添加到ViewController和self.view(parentsViewController的视图)中,并将ChildViewController的视图作为其子视图。我对childViewController的视图进行了约束。所有常量均为0,这意味着其高度和宽度应与ViewController的高度和宽度相同 详情如下: ChildViewController.view.translatesAutoresizingMaskIntoConstr

我已将ChildViewController添加到ViewController和self.view(parentsViewController的视图)中,并将ChildViewController的视图作为其子视图。我对childViewController的视图进行了约束。所有常量均为0,这意味着其高度和宽度应与ViewController的高度和宽度相同

详情如下:

  ChildViewController.view.translatesAutoresizingMaskIntoConstraints = false
  ChildViewController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
  ChildViewController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
  ChildViewController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
  ChildViewController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
当我旋转模拟器时,它们的大小是不同的

当它是肖像的时候

self.view height: 736.0
self.view is width: 414.0
ChildVC height: 414.0
ChildVC width: 736.0
当它是风景的时候

self.view height: 414.0
self.view is width: 736.0
ChildVC height: 736.0
ChildVC width: 414.0
我不确定我哪里做错了。任何帮助都将不胜感激。谢谢。

尝试在视图控制器上实现该方法,然后在视图上调用
setNeedsUpdateConstraints()

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    view.setNeedsUpdateConstraints()
    ChildViewController.view.setNeedsUpdateConstraints()
}

第一个注意事项:在发布问题时,如果您包含实际代码和所需的尽可能多的信息,这会有所帮助。问题中的代码没有显示如何加载子控制器,也没有显示如何检查视图大小

下面是一个完整的示例:

class ParentViewController: UIViewController {
    
    // save a reference to the added child controller so we can use it later
    //  we *could* get it later with:
    //      self.children.first
    //  but this helps keep things clear
    var childVCRef: ChildViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // make sure we properly load the child controller and its view
        guard let childVC = storyboard?.instantiateViewController(withIdentifier: "childVC") as? ChildViewController,
              let childView = childVC.view
        else {
            fatalError("Could not instantiate ChildViewController and view!!!")
        }
        
        self.addChild(childVC)
        
        childView.translatesAutoresizingMaskIntoConstraints = true
        view.addSubview(childView)
        NSLayoutConstraint.activate([
            childView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0),
            childView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0),
            childView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0),
            childView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0),
        ])
        
        // finish up the add child process
        childVC.didMove(toParent: self)
        
        // our saved reference
        self.childVCRef = childVC
        
        // tap anywhere to print the frames and bounds to debug console
        let t = UITapGestureRecognizer(target: self, action: #selector(self.didTap(_:)))
        view.addGestureRecognizer(t)
    }
    
    @objc func didTap(_ g: UITapGestureRecognizer) -> Void {
        if let childView = childVCRef?.view {
            print("child view frame:", childView.frame)
            print("self view frame: ", view.frame)
            print("child view bounds:", childView.bounds)
            print("self view bounds: ", view.bounds)
        }
    }
    
}

class ChildViewController: UIViewController {
}
调试输出(在iPhone 8 Plus上):

我的故事板如下所示:

下面是测试它的故事板源代码:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="kLU-os-Aay">
    <device id="retina3_5" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="System colors in document resources" minToolsVersion="11.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Parent View Controller-->
        <scene sceneID="BUx-ms-dfu">
            <objects>
                <viewController id="kLU-os-Aay" customClass="ParentViewController" customModule="TableAdd" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="aha-n9-zpP">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aNw-CK-Ha7">
                                <rect key="frame" x="30.5" y="192.5" width="259.5" height="95.5"/>
                                <string key="text">Parent
View Controller</string>
                                <fontDescription key="fontDescription" type="system" pointSize="40"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <viewLayoutGuide key="safeArea" id="EgR-uj-Wqw"/>
                        <color key="backgroundColor" systemColor="systemBackgroundColor"/>
                        <constraints>
                            <constraint firstItem="aNw-CK-Ha7" firstAttribute="centerY" secondItem="aha-n9-zpP" secondAttribute="centerY" id="Gm5-hu-1XP"/>
                            <constraint firstItem="aNw-CK-Ha7" firstAttribute="centerX" secondItem="aha-n9-zpP" secondAttribute="centerX" id="cKo-Qe-PaP"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dOa-Qb-1Sa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-49" y="301"/>
        </scene>
        <!--Child View Controller-->
        <scene sceneID="S1F-Fh-BIa">
            <objects>
                <viewController storyboardIdentifier="childVC" id="2Dw-FZ-578" customClass="ChildViewController" customModule="TableAdd" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="cUH-MF-5Zw">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UCh-4h-xoX">
                                <rect key="frame" x="30.5" y="192.5" width="259.5" height="95.5"/>
                                <string key="text">Child
View Controller</string>
                                <fontDescription key="fontDescription" type="system" pointSize="40"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <viewLayoutGuide key="safeArea" id="adq-eR-YNk"/>
                        <color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="UCh-4h-xoX" firstAttribute="centerY" secondItem="cUH-MF-5Zw" secondAttribute="centerY" id="A7L-CD-TYj"/>
                            <constraint firstItem="UCh-4h-xoX" firstAttribute="centerX" secondItem="cUH-MF-5Zw" secondAttribute="centerX" id="thX-2b-mJH"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="bTn-X0-Pmx" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="591" y="300"/>
        </scene>
    </scenes>
    <resources>
        <systemColor name="systemBackgroundColor">
            <color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
        </systemColor>
    </resources>
</document>

父母亲
视图控制器
小孩
视图控制器

还要检查视图控制器控制的要求:感谢您的帮助。对不起,还是一样的。。你介意我再补充一点信息吗?。。在模拟器上,childViewConroller的视图与ViewController的视图一样是全屏的。看起来它们的大小是一样的,但当我调用“print”方法来查看它们的帧大小时。。他们不一样。。他们走了另一条路,我又走了。现在它们的尺寸是一样的,但如果是横向的,它们的高度是736,宽度是414,如果是纵向的,它们的高度是414,宽度是736,我想应该是另一种情况。你什么时候/怎么看视图的尺寸?我刚刚做了一个快速测试,旋转后的帧(和边界)是相同的。
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="kLU-os-Aay">
    <device id="retina3_5" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="System colors in document resources" minToolsVersion="11.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Parent View Controller-->
        <scene sceneID="BUx-ms-dfu">
            <objects>
                <viewController id="kLU-os-Aay" customClass="ParentViewController" customModule="TableAdd" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="aha-n9-zpP">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aNw-CK-Ha7">
                                <rect key="frame" x="30.5" y="192.5" width="259.5" height="95.5"/>
                                <string key="text">Parent
View Controller</string>
                                <fontDescription key="fontDescription" type="system" pointSize="40"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <viewLayoutGuide key="safeArea" id="EgR-uj-Wqw"/>
                        <color key="backgroundColor" systemColor="systemBackgroundColor"/>
                        <constraints>
                            <constraint firstItem="aNw-CK-Ha7" firstAttribute="centerY" secondItem="aha-n9-zpP" secondAttribute="centerY" id="Gm5-hu-1XP"/>
                            <constraint firstItem="aNw-CK-Ha7" firstAttribute="centerX" secondItem="aha-n9-zpP" secondAttribute="centerX" id="cKo-Qe-PaP"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dOa-Qb-1Sa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-49" y="301"/>
        </scene>
        <!--Child View Controller-->
        <scene sceneID="S1F-Fh-BIa">
            <objects>
                <viewController storyboardIdentifier="childVC" id="2Dw-FZ-578" customClass="ChildViewController" customModule="TableAdd" customModuleProvider="target" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="cUH-MF-5Zw">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UCh-4h-xoX">
                                <rect key="frame" x="30.5" y="192.5" width="259.5" height="95.5"/>
                                <string key="text">Child
View Controller</string>
                                <fontDescription key="fontDescription" type="system" pointSize="40"/>
                                <nil key="textColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <viewLayoutGuide key="safeArea" id="adq-eR-YNk"/>
                        <color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="UCh-4h-xoX" firstAttribute="centerY" secondItem="cUH-MF-5Zw" secondAttribute="centerY" id="A7L-CD-TYj"/>
                            <constraint firstItem="UCh-4h-xoX" firstAttribute="centerX" secondItem="cUH-MF-5Zw" secondAttribute="centerX" id="thX-2b-mJH"/>
                        </constraints>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="bTn-X0-Pmx" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="591" y="300"/>
        </scene>
    </scenes>
    <resources>
        <systemColor name="systemBackgroundColor">
            <color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
        </systemColor>
    </resources>
</document>