所以基本上,我有一个UITextView
,我想集中在我的UIView
。这里是我下面的视图控制器代码:中心故事板在自动布局中的看法
import UIKit
class OpeningScreenViewController: UIViewController {
@IBOutlet weak var topBarUIView: UIView!
@IBOutlet weak var topViewTextView: UITextView!
override func viewDidAppear(animated: Bool) {
let superViewWidth = view.frame.size.width
let superViewHeight = view.frame.size.height
//creating the top bar
topBarUIView.frame = CGRect(x: 0, y: 0, width: superViewWidth, height: superViewHeight * 0.1)
topBarUIView.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.5)
//adding text to the top bar
topViewTextView.frame = CGRectMake(0, 0, superViewWidth, superViewHeight * 0.1)
topViewTextView.backgroundColor = UIColor.blueColor()
topViewTextView.text = "Hello World"
topViewTextView.textAlignment = NSTextAlignment.Center
topViewTextView.font = UIFont(name: "Arial", size: 24)
}
}
我改变我的背景考虑到50%的不透明度和TextView的到的蓝色背景色,但是当我运行此,蓝色背景中的UIView好,像一边是10个像素,另一边是20个像素。我不知道为什么。
我在故事板中设置它,然后将它拖动到上面,然后更改viewDidAppear中视图的属性以覆盖故事板。
我也试着做这样的事情:
override func viewDidAppear(animated: Bool) {
// Get the superview's layout
let margins = view.layoutMarginsGuide
// Pin the leading edge of myView to the margin's leading edge
topBarUIView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
// Pin the trailing edge of myView to the margin's trailing edge
topBarUIView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true
// Give myView a 1:2 aspect ratio
topBarUIView.heightAnchor.constraintEqualToAnchor(topBarUIView.widthAnchor, multiplier: 2.0)
}
,并得到一个奇怪的错误说,布局约束都出现问题。
分别设置视图的高度和宽度以及起点会更容易吗?如果是的话我怎么能这样做伪代码:
topBar.startingPoint = (x,y)
topBar.height = this
topBar.width = this
等
在stroryboard你为什么不申请限制? –
我被要求尽可能以编程的方式来做,并尽可能地避免故事板。 – dnaland
请使用自动布局,否则它会让你处理混乱。我不知道它的项目要求是不使用自动布局还是有人告诉过你。如果有人告诉我比你告诉你从体验到错误的方向,你以下是 –