ios - 带有文本和图像的 UIBarButtonItem - iOS 8

标签 ios xamarin.ios

阅读前先查看更新

我想在 iOS 8 应用程序的工具栏中放置一个按钮。该按钮需要有图像和文本。

这看起来是一个标准的做法,标准的 Apple 应用程序有很多例子。查看“时刻”及其照片、共享和相册

我看到很多关于它是否可以完成的问题。有人说没有其他人说你需要一个自定义 View 。风俗?真的吗?对于这种正常的事情。

它尝试设置标题和图像,但标题不显示

var customImageBarButtonItem = new UIBarButtonItem (UIImage.FromBundle ("tools_icon"), UIBarButtonItemStyle.Plain, OnBarButtonItemClicked);
                customImageBarButtonItem.TintColor = ApplicationColors.Purple;
                customImageBarButtonItem.Title = "Caption";
                return customImageBarButtonItem;

我正在使用 Xamarin.iOS

更新

在这个 Xamarin Forums post 中已经向我指出了这一点我认为 iOS Moments 应用程序中的工具栏按钮实际上是一个标签栏,它们确实支持开箱即用的文本和图像

最佳答案

当他们说您必须创建一个自定义 View 时,他们的意思可能是它可以像创建一个带有图像和图像左侧文本的 UIButton 一样简单,并将其用作您的“自定义 View ” View ”为您的 UIBarButtonItem。从技术上讲,UIButton 是一个 View ,因为它继承自 UIView,因此您可以将其用作自定义 View 。

//Create a UIButton with an image on the left, and text to the right
var buttonImage = UIImage.FromBundle("tools_icon");
var button = new UIButton(UIButtonType.Custom)
{
     Frame = new CGRect(0, 0, 120, 33) //You may need to adjust as necessary
}
;
button.SetImage(buttonImage, UIControlState.Normal);
button.SetTitle("Caption", UIControlState.Normal);
button.SetTitleColor(UIColor.Black, UIControlState.Normal);

//create a UIBarButtonItem with a UIButton as the custom view.
var barButtonItem = new UIBarButtonItem(button);
NavigationItem.RightBarButtonItem = barButtonItem;

根据“tools_icon”的大小,您可能需要进行任何必要的调整,但按钮框架的宽度必须足够长以包含图像图标和文本。

关于ios - 带有文本和图像的 UIBarButtonItem - iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206696/

相关文章:

ios - 使用 Swift 检查 ios 中蜂窝网络的互联网连接

ios - 下载 JSON 异步崩溃

ios - 如何使用 AFNetworking 3.0 发送批量请求

iphone - 更改 EntryElement 键盘 returnkeytype monotouch.dialog

iphone - :In app purchase and Monotouch 的文档或博客

xamarin.ios - 无法在设备上启动 Xamarin.iOS 应用

ios - 在不获取的情况下增加 CKRecord 变量中的字段值?

ios - 使用选项卡式应用程序中的相同选项卡打开 View Controller

visual-studio - Xamarin Visual Studio 无法在联网 MAC 上启动构建服务器激活

c# - 从命令行完全构建 Monotouch 应用程序包的完整步骤集是什么?