0
我有代碼:UIScrollView頁面寬度不等於self.view寬度
爲什麼UIScrollView的頁面寬度不等於self.view寬度?
如果我認爲正確,self.view.frame.size.width必須等於scroll.bounds.width且scroll.contentSize必須爲self.view.frame.size.width * 4(在這種情況下),這樣對嗎?
Thx很多!
class ViewController: UIViewController {
@IBOutlet weak var scroll: UIScrollView!
var frame: CGRect = CGRectMake(0, 0, 0, 0)
override func viewDidLoad() {
super.viewDidLoad()
scroll.bounds = self.view.bounds
scroll.frame = self.view.frame
NSLog("%@", UIScreen.mainScreen().bounds.width);
NSLog("%@", scroll.bounds.width);
NSLog("%@", self.view.bounds.width);
NSLog("%@", scroll.contentSize.width);
let colors = [UIColor.redColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.magentaColor()];
for index in 0..<colors.count {
frame.origin.x = self.view.frame.size.width * CGFloat(index)
frame.size = self.view.frame.size
var subView = UIView(frame: frame)
subView.backgroundColor = colors[index]
subView.layer.borderColor = UIColor.blackColor().CGColor
subView.layer.borderWidth = 1.0;
self.scroll .addSubview(subView)
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(colors.count), self.view.frame.size.height)
}
...
}
我在第二頁看到了什麼:
THX一個很多!你太棒了。是否因爲自動佈局而發生? – Costa
很酷的解釋。謝謝 –