基本的に、私はUIView
を中心にしたいUITextView
を持っています。ここでは以下の私のビューコントローラのコードは次のとおりです。オートストーリーボードビュー自動レイアウト
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)
}
とレイアウトの制約が問題を持ったという奇妙なエラーが発生しました。
ビューの高さと幅と開始点を別々に設定する方が簡単でしょうか?あなたが直接.frame
を設定すべきではありません最新のアプリケーションでは
topBar.startingPoint = (x,y)
topBar.height = this
topBar.width = this
など
あなたはstroryboardに制約を適用していないのはなぜ? –
私は可能な限りプログラム的にそれを行い、できるだけストーリーボードを避けるように求められました。 – dnaland
親切にautolayoutを使うか、扱うのが面倒になるでしょう。あなたのプロジェクト要件が、あなたにオートレイアウトを使わないように言われているのかどうか分かりません。誰かが私にあなたの経験から、次のとおりです。 –