繁体   English   中英

工具栏(iOS)上UIBarButtonItem右侧的图像图标

[英]Image icon on the right side of UIBarButtonItem on toolbar (iOS)

如何在工具栏上的UIBarButtonItem的右侧显示图像图标?

    let button = UIBarButtonItem(
        image_name: "next_item",
        title: "Continue",
        target: self,
        action_selector: nil
    )

我试图操纵button.imageInsets ,但对我来说却什么都没有改变。

Swift 3,iOS 8以上版本。

我期望这样的结果: 在此处输入图片说明

您可以使用自定义视图初始化UIBarButton。

https://developer.apple.com/reference/uikit/uibarbuttonitem/1617151-init

在自定义视图中,在旁边放一个UILabel ,然后放UIImageView

使用归因字符串,然后将图像与字符串一起添加并显示在标题上

// create an NSMutableAttributedString that we'll append everything to
let fullString = NSMutableAttributedString(string: "Start of text")

// create our NSTextAttachment
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named: "Icon.png")

// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)

// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(image1String)
fullString.append(NSAttributedString(string: "End of text"))

// draw the result in a label
yourLabel.attributedText = fullString

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM