在故事板源代码中使用 titleView
using titleView in storyboard source code
我想在我的 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
想法?
不要尝试将故事板源 XML 编辑为文本,尤其是对于如此复杂的内容。你不可能正确地做到这一点。 (一方面,您需要插入所有必要的 id
属性信息:我注意到您没有这样做。)编辑故事板的方法是在图形故事板编辑器中。如果您没有以图形方式看到故事板,control-click 在项目导航器中选择它并选择打开为 -> 界面生成器。
你也说
when I try to add a titleView barButtonItem
titleView
不能是 UIBarButtonItem。它必须是一个 UIView,而 UIBarButtonItem 不是 UIView。因此,要在故事板中配置它,将所需的视图(或视图子类)添加到场景中,并将导航项的 titleView
出口连接到它。
成功了。 @matt 的 "titleView cannot be a UIBarButtonItem. It must be a UIView" 帮助我让它工作,但它的指导还不够。 [注意:我在 Xamarin iOS 应用程序上 Visual Studio 为 Mac 工作。]
出于某种原因,Designer 每次都将我拖动到 navigationItem 的 UIView 包装在 barButtonItem 中。 - 导致 xml 就像我原来的 post.
所以,我删除了 barButtonItem 元素,并替换了这个
<view key="customView"
有了这个
<view key="titleView"
对不起,如果我没有以正确的方式做到这一点。如果您必须再给我一个 -1,那么我想您必须这样做。
: - )
我想在我的 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
想法?
不要尝试将故事板源 XML 编辑为文本,尤其是对于如此复杂的内容。你不可能正确地做到这一点。 (一方面,您需要插入所有必要的 id
属性信息:我注意到您没有这样做。)编辑故事板的方法是在图形故事板编辑器中。如果您没有以图形方式看到故事板,control-click 在项目导航器中选择它并选择打开为 -> 界面生成器。
你也说
when I try to add a titleView barButtonItem
titleView
不能是 UIBarButtonItem。它必须是一个 UIView,而 UIBarButtonItem 不是 UIView。因此,要在故事板中配置它,将所需的视图(或视图子类)添加到场景中,并将导航项的 titleView
出口连接到它。
成功了。 @matt 的 "titleView cannot be a UIBarButtonItem. It must be a UIView" 帮助我让它工作,但它的指导还不够。 [注意:我在 Xamarin iOS 应用程序上 Visual Studio 为 Mac 工作。]
出于某种原因,Designer 每次都将我拖动到 navigationItem 的 UIView 包装在 barButtonItem 中。 - 导致 xml 就像我原来的 post.
所以,我删除了 barButtonItem 元素,并替换了这个
<view key="customView"
有了这个
<view key="titleView"
对不起,如果我没有以正确的方式做到这一点。如果您必须再给我一个 -1,那么我想您必须这样做。
: - )