文本无法正确显示.text应放在屏幕中央以显示该功能

时间:2016-02-02 02:30:25

标签: ios swift

我正试图在一个人注册时显示文字。下一个屏幕用一个文字"检查证书......"但是它的功能不是在屏幕中间显示文本

func rotatingspinner()
    {
        processingView = UIView()

        processingView.frame = self.view.bounds

        processingView.backgroundColor = UIColor(red: 0.0/255.0, green: 113.0/255.0 , blue: 138.0/255.0, alpha: 0.97)

        processLabel = UILabel()



        let device = Device()


        if device == .iPhone6sPlus {
            processLabel.frame = CGRectMake(145,410,160,30)
        }
        if device == .iPhone6s {
            processLabel.frame = CGRectMake(125,375,160,30)
        }
        if device == .iPhone6Plus {
            processLabel.frame = CGRectMake(145,410,160,30)
        }
        if device == .iPhone6 {
            processLabel.frame = CGRectMake(125,375,160,30)
        }

        if device == .iPhone5s {
            processLabel.frame = CGRectMake(100,325,160,20)
        }
        if device == .iPhone5 {
            processLabel.frame = CGRectMake(100,325,160,20)
        }
        if device == .iPhone4s {
            processLabel.frame = CGRectMake(100,275,160,20)
        }
        if device == .iPhone4 {
            processLabel.frame = CGRectMake(100,275,160,20)
        }

        processLabel.text = "Checking Credentials......"
        processLabel.font = UIFont(name: "Arial", size: 15.0)
        processLabel.textColor = UIColor.whiteColor()
        processingView.addSubview(processLabel)
        self.processingView.addSubview(activityIndicator)
        activityIndicator.center = self.processingView.center
        activityIndicator.startAnimating()
        self.view.addSubview(processingView)

    }

如何将screensize.window设置为框架并设置框架以使文本保持在中间

1 个答案:

答案 0 :(得分:1)

正如尼克所说,你应该开始学习自动布局。来到你的代码,你真的不需要拥有这一切。您需要为标签计算框架以确保其位于中心。你可以按照以下方式做到这一点

let labelHeight = CGFloat(30)
let labelWidth = CGFloat(160)
let labelX = (processingView.frame.size.width - labelHeight)/2
let labelY = (processingView.frame.size.height - labelWidth)/2
processLabel.frame = CGRectMake(labelX, labelY, labelWidth, labelHeight)