如何在条形按钮项目上放置标签?

时间:2018-12-26 11:30:30

标签: ios swift

我想在导航栏上制作日历
喜欢图片上的
enter image description here

但是我不知道如何在日历图像上放置标签
请帮帮我
谢谢

1 个答案:

答案 0 :(得分:0)

使用以下代码在导航栏中添加图像并在其上添加标签:

let button =  UIButton(type: .custom)
        button.setImage(UIImage(named: "calenderImage"), for: .normal)
        button.addTarget(self, action: Selector(("buttonAction")), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 53, height: 31) 
        let label = UILabel(frame: CGRect(x: 3, y: 5, width: 50, height: 20))// set position of label
        label.font = UIFont(name: "Arial-BoldMT", size: 16)// add font and size of label
        label.text = "your date number"
        label.textAlignment = .center
        label.textColor = UIColor.black
        label.backgroundColor =   UIColor.clear
        button.addSubview(label)
        let barButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = barButton