我们如何向 UINavigation 添加额外的 UILabels? [英] How we can add additional UILabels to UINavigation?

查看:30
本文介绍了我们如何向 UINavigation 添加额外的 UILabels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 UINavigation 添加一个循环视图和一个标签.像这样:

我可以通过以下代码为我的 UINavigation 设置标签:

 if let navigationBar = self.navigationController?.navigationBar {让 firstFrame = CGRect(x: 300, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)让 firstLabel = UILabel(frame: firstFrame)firstLabel.text = "第一"navigationBar.addSubview(firstLabel)}

但是这段代码有两个问题:1.如何正确设置x位置?(为了测试我设置了 300 值,但是这个值在不同的屏幕尺寸上显示不同的位置)2.如何给这个标签设置循环背景?

解决方案

您可以以编程方式将视图(红色圆圈)和标签(编号 16)作为子视图添加到栏按钮项的按钮中.

>

你应该做的是:

  • 将按钮作为 IBOutlet 连接到其 ViewController:

确保连接的组件是 UIButton,而不是 UIBarButtonItem.

如你所见,我称之为btnMenu.

  • 创建您的视图(红色圆圈和数字标签)并将其添加到btnMenu:

它应该类似于:

导入 UIKit类视图控制器:UIViewController {//...@IBOutlet 弱变量 btnMenu:UIButton!覆盖 func viewDidLoad() {super.viewDidLoad()//...//设置红色圆圈 UIView让 redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))redCircleView.backgroundColor = UIColor.redredCircleView.layer.cornerRadius = view.frame.size.width/2//设置数字 UILabellet label = UILabel(frame: CGRect(x: 4, y: 0, width: 20, height: 20))label.textColor = UIColor.whitelabel.font = UIFont.systemFont(ofSize: 10)label.text = "16"//将标签添加到红色圆圈中redCircleView.addSubview(label)//将红色圆圈添加到菜单按钮中btnMenu.addSubview(redCircleView)//...}//...}

就是这样!

希望这有帮助.

I would like to add a cycle view and a label to UINavigation. like this:

I can set a label to my UINavigation by this code:

    if let navigationBar = self.navigationController?.navigationBar {
        let firstFrame = CGRect(x: 300, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)
        let firstLabel = UILabel(frame: firstFrame)
        firstLabel.text = "First"

        navigationBar.addSubview(firstLabel)

    }

but I have two problems by this code: 1.how to set x position correctly? (to test I set 300 value, but this value show different position on different screen sizes) 2. how to set a cycle background to this label ?

解决方案

You can add both of the view (red circle) and the label (number 16) programmatically as a subView to the button of the bar button item.

What you should do is:

  • Connect the button as an IBOutlet to its ViewController:

Make sure that the connected component is the UIButton, but NOT UIBarButtonItem.

As you can see, I called it btnMenu.

  • Create your views (red circle and number label) and add it to the btnMenu:

It should be similar to:

import UIKit

class ViewController: UIViewController {
    //...

    @IBOutlet weak var btnMenu: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        //...

        // 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: 4, 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)

        //...
    }

    //...
}

And that's it!

UIButton is a subclass of UIView, meaning that you can add subviews to it (add​Subview(_:​)).

Output:

Hope this helped.

这篇关于我们如何向 UINavigation 添加额外的 UILabels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆