我试图让一些文字居中,但似乎没有成功。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let title = UILabel()
title.text = "Some Sentence"
title.numberOfLines = 0
title.frame = CGRectMake(self.view.bounds.size.width/2,50,self.view.bounds.size.width, self.view.bounds.size.height) // x , y, width , height
title.textAlignment = .Center
title.sizeToFit()
title.backgroundColor = UIColor.redColor()
self.view.addSubview(title)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我正在使用的代码,但这是我得到的结果:
它没有居中显示在屏幕上。有人能告诉我我做错了什么吗?
title.center.x = self.view.center.x
用于y:title.center.y = self.view.center.y
- Rashwan LviewForHeaderInSection
委托方法自定义我的标题视图,但它并没有像你所提到的那样将标签居中。我设置了框架,如下:label.frame = CGRect(x: self.view.frame.size.width/2, y: self.view.bounds.height/2, width: 80, height: 20),并在它之前添加了你的代码行。 - Usama bin AttiquetableView.center.y
和tableView.center.x
。 - Rashwan L