OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将按钮设置到导航的最右侧 - iOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:19
标题: ios - 如何将按钮设置到导航的最右侧 - iOS

我在导航栏右侧设置了一个按钮:

我的 View Controller : enter image description here

我想为这个按钮设置一个汉堡图标、红色循环和一个标签。像这样:

enter image description here

我的代码:

    self.navigationController?.navigationBar.barTintColor = self.utilities.hexStringToUIColor(hex: "#00b8de")

    var imageview2 = UIImage(named: "menulogo")

    imageview2 = imageview2?.imageResize(sizeChange: CGSize(width: 25, height: 25))

    btnMenu.setImage(imageview2,for:UIControlState.normal)
    btnMenu.setTitle("", for: .normal)


    // setup the red circle UIView
    let redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    redCircleView.backgroundColor = UIColor.red
    redCircleView.layer.cornerRadius = view.frame.size.width / 2

    // setup the number UILabel
    let label = UILabel(frame: CGRect(x: 30, y: 0, width: 20, height: 20))
    label.textColor = UIColor.white
    label.font = UIFont.systemFont(ofSize: 10)
    label.text = "16"

    // adding the label into the red circle
    redCircleView.addSubview(label)

    // adding the red circle into the menu button
    btnMenu.addSubview(redCircleView)

使用上面的代码我有三个问题:

  1. 我的汉堡图片不在导航的最右侧。
  2. 我的自行车 View 没有显示
  3. 我的图标是白色的,但它显示为蓝色!

btnMenu 是我的导航按钮。

enter image description here



Best Answer-推荐答案


我已经使用这个库在导航按钮上设置角标(Badge)。

MIBadgeButton is badge button written in Swift with high UITableView/UICollectionView performance.

https://github.com/mustafaibrahim989/MIBadgeButton-Swift

关于ios - 如何将按钮设置到导航的最右侧 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42898344/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4