【问题标题】:using titleView in storyboard source code在故事板源代码中使用 titleView
【发布时间】:2025-11-28 19:15:02
【问题描述】:

我想在我的 navigationItem 中有一个 titleView,这样我就可以在屏幕顶部看到这样的内容。

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

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

想法?

【问题讨论】:

    标签: ios uinavigationbar


    【解决方案1】:

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

    你也说

    当我尝试添加 titleView barButtonItem 时

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

    【讨论】:

    • 感谢您的指导。我现在已经完成了所有这些,除了我不知道该怎么做才能“将​​导航项的 titleView 插座连接到”我现在添加的 UIView。请提示。
    • 添加了我做的截图。
    • 注意:我在 Xamarin iOS 应用上的 Visual Studio for Mac 中工作。出于某种原因,Designer 每次都将我拖到 navigationItem 的 UIView 包装在 barButtonItem 中。 - 导致像我原来的帖子那样的xml。请参阅下面的更多详细信息。
    • 好吧,就像我在回答中告诉你的那样,这不是你所做的。您将视图拖到 场景 中,而不是拖到导航栏中,现在您可以将插座连接到它。
    【解决方案2】:

    搞定了。 @matt 的“titleView 不能是 UIBarButtonItem。它必须是 UIView”帮助我让它工作,但它没有足够的指导。 [注意:我在 Xamarin iOS 应用上使用 Visual Studio for Mac 工作。]

    出于某种原因,设计器每次都将我拖到导航项的 UIView 包装在 barButtonItem 中。 - 在我原来的帖子中产生这样的 xml。

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

    <view key="customView"
    

    有了这个

    <view key="titleView"
    

    对不起,如果我没有以正确的方式做到这一点。如果你必须给我另一个-1,那么我想你必须。

    : - )

    【讨论】: