ios - 带有异步加载图像的按钮作为 UINavigationItem 中的 titleView

标签 ios uibutton uikit uinavigationbar kingfisher

我正在尝试从 Tinder 应用程序重现这种导航栏: enter image description here

所以我尝试将按钮设置为 UINavigationItem 的 titleView,并带有标题和图像。我使用以下类别来对齐图像下的标题:

extension UIButton {

    func centerButtonVertically(padding: CGFloat = 6.0) {
        guard
            let imageViewSize = self.imageView?.frame.size,
            let titleLabelSize = self.titleLabel?.frame.size else {
                return
        }

        let totalHeight = imageViewSize.height + titleLabelSize.height + padding

        self.imageEdgeInsets = UIEdgeInsets(
            top: -(totalHeight - imageViewSize.height),
            left: 0.0,
            bottom: 0.0,
            right: -titleLabelSize.width
        )

        self.titleEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: -imageViewSize.width,
            bottom: -(totalHeight - titleLabelSize.height),
            right: 0.0
        )

        self.contentEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: 0.0,
            bottom: titleLabelSize.height,
            right: 0.0
        )
    }
}

我正在使用 Kingfisher 下载图像、调整图像大小并使其圆整:

override func viewDidLoad() {
    super.viewDidLoad()

    let rounded = RoundCornerImageProcessor(cornerRadius: 15)
    let downsampling = ResizingImageProcessor(referenceSize: CGSize(width: 30.0, height: 30.0), mode: .aspectFit)
    matchAvatarButton.setTitle(self.match.shortName, for: .normal)
    matchAvatarButton.contentMode = .scaleAspectFit
    matchAvatarButton.kf.setImage(with: self.match.pictureUrls[0] as Resource, for: .normal, placeholder: nil, options: [.processor(downsampling), .processor(rounded)])
    matchAvatarButton.centerButtonVertically()
    ...
}

请注意,该按钮被设置为 Storyboard 中导航栏的 titleView,然后我通过 viewDidLoad 中的导出引用修改该按钮。

但结果与我想要达到的目标相去甚远:

enter image description here

图像保持矩形,我希望它是圆形的。由于某种奇怪的原因,我看不到标题。

知道我错过了什么吗?

最佳答案

结果如下: Custom Title

  1. 在 Storyboard的导航栏中放置一个 UIView 而不是一个按钮。
  2. 将按钮和标题放在该 View 中。 (将按钮类型设置为自定义)
  3. 获取导航栏高度
  4. RoundCornerImageProcessor 使用自定义初始化

代码如下:

class ViewController: UIViewController {

        @IBOutlet weak var centerView: UIView!
        @IBOutlet weak var imageButton: UIButton!
        @IBOutlet weak var label: UILabel!

        override func viewDidLoad() {
            super.viewDidLoad()

            // Custom sizes
            let centerViewWidth = 120
            let imageSide = 30

            // Custom init gives a rounded image
            let rounded = RoundCornerImageProcessor(cornerRadius: CGFloat(imageSide/2), targetSize: CGSize(width: imageSide,height: imageSide), roundingCorners: RectCorner(arrayLiteral: .all), backgroundColor: .clear)
            // Downsample to two times the container size to get a better resolution
            let downsampling = ResizingImageProcessor(referenceSize: CGSize(width: imageSide*2, height: imageSide*2), mode: .aspectFit)


            // Setup frame and positions
            let centerViewHeight = Int(self.navigationController?.navigationBar.frame.height ?? 50)
            centerView.frame = CGRect(x: 0, y: 0, width: centerViewWidth, height: centerViewHeight)
            imageButton.frame = CGRect(x: centerViewWidth/2-imageSide/2, y: 0, width: imageSide, height: imageSide)
            label.frame = CGRect(x: 0, y: imageSide, width: centerViewWidth, height: centerViewHeight-imageSide)

            // Quick image download
            let imgUrl = "https://via.placeholder.com/150/771796"
            let resource = ImageResource(downloadURL: URL(string: imgUrl)!)
            imageButton.kf.setImage(with: resource, for: .normal, placeholder: nil, options: [.processor(downsampling), .processor(rounded)])

            // Setup title
            label.text = "Custom title!"
            label.textAlignment = .center

        }
    }

关于ios - 带有异步加载图像的按钮作为 UINavigationItem 中的 titleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56815991/

相关文章:

swift - 应用程序在计时器后崩溃而不是重新启用按钮

ios - UITextView滚动时 float 蓝框

ios - 如何使用 NSRegularExpression 或 Objective-C 中的任何其他有效方式检查 GUID(或 UUID)的有效性

ios - 未找到自动链接框架领域

c# - MonoTouch.Dialog:部分中的 GlassButton

ios - 以编程方式循环 UIButton

iphone - 添加为 subview 时 UITextField 不显示

iphone - 需要帮助了解如何以编程方式添加 IB 创建的 UIView

ios - AFNetworking 与不可靠的 wifi - 检测/重试连接超时

ios - 过滤后填充uitableviewcell,单元格内容始终相同