Xib显示空屏幕虽然它在设计师

时间:2017-07-19 19:05:10

标签: ios xamarin xib designer

我真的不明白我在这里做错了什么。我设计了一个非常简单的.xib文件,显示了一个textview和一个按钮。在设计师,它工作得很好。缩放的工作方式与预期的一样。但是当我在模拟器中运行程序时,我只得到一个空屏幕。有几次我看到按钮的下半部分位于导航栏的下方,这使我得出的结论是视图必须位于可视区域外的顶部。

正如您在下面的图片中看到的,我在视图中添加了几个约束,可以很好地缩放设计器中的视图。我试过添加顶部间距。包含StackView中的视图但似乎没有什么可以做到的。我错过了什么?

以下是使用MvvmCross启动视图的代码。

    /// <summary>
    /// Call the MvvmCross ShowViewModel method 
    /// </summary>
    public bool Show<TViewModel>(IMvxBundle presentationBundle = null)
        where TViewModel : IMvxViewModel
    {
        return base.ShowViewModel<TViewModel>(presentationBundle: presentationBundle);
    }

    /// <summary>
    /// Show a view with viewmodel
    /// </summary>
    public bool Show<TViewModel>()
        where TViewModel : IMvxViewModel
    {
        return Show<TViewModel>();
    }

演示文稿在自定义视图展示器中处理:

using MvvmCross.Core.ViewModels;
using MvvmCross.iOS.Views;
using MvvmCross.iOS.Views.Presenters;
using MvvmCross.Platform;
using Notifier.Classes;
using Notifier.Interfaces;
using UIKit;
using static Notifier.Classes.ViewModelController;

namespace Notifier.iOS.Classes.MvvmCross
{
    public class ViewPresenter : MvxIosViewPresenter
    {
        private IMvxIosViewCreator _viewCreator;

        private IPlatformLog _log;
        private IPlatformLog Log => _log == null ? _log = Mvx.Resolve<IPlatformLog>() : _log;

        protected IMvxIosViewCreator ViewCreator
        {
            get { return _viewCreator ?? (_viewCreator = Mvx.Resolve<IMvxIosViewCreator>()); }
        }

        public ViewPresenter(IUIApplicationDelegate applicationDelegate, UIWindow window)
            : base(applicationDelegate, window)
        {
        }

        public override void Show(MvxViewModelRequest request)
        {
            base.Show(request);
        }
    }
}

这是.xib的内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="HomeView">
            <connections>
                <outlet property="view" destination="2" id="RRd-Eg-VrN"/>
                <outlet property="Registration" destination="11" id="name-outlet-11"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="2" horizontalHuggingPriority="1">
            <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            <subviews>
                <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" id="10" translatesAutoresizingMaskIntoConstraints="NO">
                    <rect key="frame" x="16" y="16" width="568" height="128"/>
                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                    <string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
                    <fontDescription key="fontDescription" type="system" pointSize="14"/>
                    <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
                </textView>
                <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="11" translatesAutoresizingMaskIntoConstraints="NO">
                    <rect key="frame" x="16" y="188" width="568" height="30"/>
                    <state key="normal" title="Button">
                        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                    </state>
                </button>
            </subviews>
            <constraints>
                <constraint id="14" firstItem="2" firstAttribute="trailing" secondItem="10" secondAttribute="trailing" constant="16"/>
                <constraint id="15" firstItem="10" firstAttribute="leading" secondItem="2" secondAttribute="leading" constant="16"/>
                <constraint id="16" firstItem="11" firstAttribute="leading" secondItem="2" secondAttribute="leading" constant="16"/>
                <constraint id="17" firstItem="2" firstAttribute="trailing" secondItem="11" secondAttribute="trailing" constant="16"/>
            </constraints>
        </view>
    </objects>
    <simulatedMetricsContainer key="defaultSimulatedMetrics">
        <simulatedScreenMetrics key="destination" type="retina47"/>
    </simulatedMetricsContainer>
</document>

这是设计师的通用视图:

enter image description here

这是特定iPhone的设计师:

enter image description here

这就是我在模拟器中看到的:

enter image description here

1 个答案:

答案 0 :(得分:1)

应该是由于约束问题。请检查您的约束是否正确定义。正确定义约束后,只有在xib中选择视图时才会看到蓝线。

从屏幕截图中,您只定义了前导和尾随约束。还请定义顶部和高度约束。