在情节提要源代码中使用titleView

时间:2019-05-07 16:01:16

标签: ios uinavigationbar

我想在我的navigationItem中有一个titleView,以便在屏幕顶部获得类似的内容。

Desired nav bar in UI

我已经找到了如何在代码中执行此操作,例如

navigationItem.titleView = MyCustomUiView

但是我想在情节提要的源代码中做到这一点。

我可以正确显示rightBarButtonItem,但是当我尝试添加titleView barButtonItem时,xml将无法解析。

这是我正在尝试的:

                   <navigationItem key="navigationItem" id="ddL-ut-3b9">    
                    <barButtonItem key="titleView" style="plain" id="11600">
                        <view key="customView" contentMode="scaleToFill" id="11599">
                            <rect key="frame" x="0.0" y="0.0" width="140" height="44"/>
                            <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" flexibleMaxY="YES" flexibleMinY="YES"/>
                            <color key="backgroundColor" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace" white="0" alpha="0"/>                                                                
                            <subviews>
                                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Disclaimer" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="11601">
                                    <rect key="frame" x="0.0" y="0.0" width="116" height="37"/>
                                    <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" flexibleMaxY="YES" flexibleMinY="YES"/>
                                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                    <color key="textColor" colorSpace="custom" customColorSpace="sRGB" red="0.982638542758316" green="1" blue="0.96209174603214" alpha="1"/>                                        <nil key="highlightedColor"/>
                                    <color key="backgroundColor" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace" white="0" alpha="0"/>                                    </label>
                            </subviews>
                        </view>
                    </barButtonItem>               
                   <barButtonItem key="rightBarButtonItem" title="Log Out" style="plain" id="o6O-LC-29z">
                        <color key="tintColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
                        <connections>                                <action selector="btnLogOut:" destination="24s-67-KDm" id="8tF-zL-Lq7"/>
                        </connections>
                    </barButtonItem>
                </navigationItem>

我在做什么错了?

这是唯一的一个,显示很好

 <barButtonItem key="rightBarButtonItem" 

但是当我添加

 <barButtonItem key="titleView"

我明白了,无法解析xml

想法?

2 个答案:

答案 0 :(得分:0)

不要尝试将情节提要源XML编辑为文本,尤其是对于如此复杂的内容。您可能无法正确执行。 (一方面,您需要插入所有必要的id属性信息:我注意到您没有这样做。)编辑故事板的方法是在图形故事板编辑器中。如果没有以图形方式看到情节提要,请在项目导航器中按住Control键并单击它,然后选择“打开方式--界面生成器”。

你也说

  

当我尝试添加titleView barButtonItem

titleView不能是UIBarButtonItem。它必须是UIView,并且UIBarButtonItem不是UIView。因此,要在情节提要中对其进行配置,请向场景中添加所需的视图(或视图子类),然后将导航项目中的titleView出口与其连接。

enter image description here

答案 1 :(得分:0)

使其正常工作。 @matt的“ titleView不能是UIBarButtonItem。它必须是UIView”可以帮助我使其正常运行,但是这还不够。 [注意:我正在Xamarin iOS应用上的Visual Studio for Mac中工作。]

由于某种原因,设计器每次都将我拖动到UIItem的UIView包装在barButtonItem中。 -在我的原始帖子中生成了xml。

所以,我删除了barButtonItem元素,并替换了它

<view key="customView"

与此

<view key="titleView"

对不起,如果我做的方法不正确。如果您必须再给我-1,那么我想您必须这样做。

:-)