我是iOS
自動佈局的新手。我想使用UIScrollView
啓用尋呼來實現查看尋呼機。我將scrollView設置爲(w,h)爲(200,65),但scrollView的寬度在運行時始終爲240。結果,內容的偏移量,我添加到scrollView中的標籤與scrollView的寬度不匹配。他們爲什麼不同?我如何獲得正確的寬度?iOS UIScrollView的寬度與約束中設置的寬度不同
private let contents = ["This is the 1st message",
"This is the 2nd message",
"This is the 3rd message"]
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
print("scrollView.bounds = \(scrollView.bounds)")
print("scrollView.frame = \(scrollView.frame)")
var x: CGFloat = 0
for content in contents {
let label = UILabel(frame: CGRectMake(x, 0, 0, 0))
label.text = content
label.sizeToFit()
print("label.frame = \(label.frame)")
scrollView.addSubview(label)
x += scrollView.bounds.width
}
scrollView.contentSize = CGSizeMake(x, scrollView.bounds.height)
}
Output:
scrollView.bounds = (0.0, 0.0, 240.0, 128.0)
scrollView.frame = (80.0, 156.0, 240.0, 128.0)
label.frame = (0.0, 0.0, 178.0, 20.3333333333333)
label.frame = (240.0, 0.0, 185.666666666667, 20.3333333333333)
label.frame = (480.0, 0.0, 182.333333333333, 20.3333333333333)
添加scrollview約束x和y位置。 –
你當前的大小課程是什麼? –
@MarcoSantarossa我當前的尺寸是wCompact hRegular –