使用自动布局的滚动视图大小:compact/Any 大小 class 中的框架为 600x480,大于 iphone5?
scrollview size with Autolayout: frame is 600x480 in compact/Any size class, bigger than iphone5?
我希望滚动视图的框架与超级视图的框架大小相同。并设置内容大小,使其包含所有 imageView。
AutoLayout 的问题,宽度和高度是 600 x 480,我使用 iphone5,大小 class 是 w:Compact / h:Any。(我期望是这样的:320 x 523).
我的约束是:与 superview 等宽,与 superview 等高(乘数 0.8),水平中心 X 和垂直中心 Y。
let pagesScrollViewSize = scrollView.frame.size
println("pagesScrollViewSize : \(pagesScrollViewSize)") //600x480
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
有人知道怎么解决吗? imageview 看起来比屏幕的框架大,所以 .SizeAspectFit 没有遵循正确的尺寸。它没有滚动到另一个 imageView,而是滚动查看图像的其余部分,这是我不想要的。
编辑:
这是 ViewController.swift 中的全部代码:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var pageViews: [UIImageView?] = []
var pageImages : [UIImage?] = []
override func viewDidLoad() {
super.viewDidLoad()
pageImages = [UIImage(named:"photo1.png"),
UIImage(named:"photo2.png"),
UIImage(named:"photo3.png"),
UIImage(named:"photo4.png")]
let pageCount = pageImages.count
pageControl.currentPage = 0
pageControl.numberOfPages = pageCount
for _ in 0..<pageCount {
pageViews.append(nil)
}
let pagesScrollViewSize = scrollView.frame.size
println("pagesScrollViewSize : \(pagesScrollViewSize)")
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
loadVisiblePages()
}
//when scrolling
func scrollViewDidScroll(scrollView: UIScrollView!){
loadVisiblePages()
}
func loadVisiblePages(){
let pageWidth = scrollView.frame.size.width
let page = Int(floor( (scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth*2.0) ))
println("page: \(page)")
pageControl.currentPage = page
let firstPage = page-1
let lastPage = page+1
//remove all the pages before firstPage
for var index = 0; index < firstPage; ++index{
purgePage(index)
}
//load pages in our range
for var index = firstPage; index <= lastPage; ++index {
loadPage(index)
}
//remove after lastPage
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}
func loadPage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
//already loaded
}
else {
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(index)
frame.origin.y = 0.0
println("\(frame)")
var newImageView = UIImageView(image:pageImages[index])
newImageView.contentMode = .ScaleAspectFit
newImageView.frame = frame
scrollView.addSubview(newImageView)
pageViews[index] = newImageView
}
}
func purgePage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
pageView.removeFromSuperview()
pageViews[index] = nil
}
}
}
谢谢
大小 类(或任何自动布局更改,就此而言)在 viewDidAppear
或 viewDidLayoutSubviews
之后才会应用。您需要将此代码移至其中一个。您看到的是视图的框架,因为它是从 Interface Builder 文档(xib 或故事板)加载出来的。在这些生命周期事件之后,您永远不应该检查帧。
Unable to set frame correctly before viewDidAppear
我希望滚动视图的框架与超级视图的框架大小相同。并设置内容大小,使其包含所有 imageView。
AutoLayout 的问题,宽度和高度是 600 x 480,我使用 iphone5,大小 class 是 w:Compact / h:Any。(我期望是这样的:320 x 523).
我的约束是:与 superview 等宽,与 superview 等高(乘数 0.8),水平中心 X 和垂直中心 Y。
let pagesScrollViewSize = scrollView.frame.size
println("pagesScrollViewSize : \(pagesScrollViewSize)") //600x480
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
有人知道怎么解决吗? imageview 看起来比屏幕的框架大,所以 .SizeAspectFit 没有遵循正确的尺寸。它没有滚动到另一个 imageView,而是滚动查看图像的其余部分,这是我不想要的。
编辑:
这是 ViewController.swift 中的全部代码:
class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
var pageViews: [UIImageView?] = []
var pageImages : [UIImage?] = []
override func viewDidLoad() {
super.viewDidLoad()
pageImages = [UIImage(named:"photo1.png"),
UIImage(named:"photo2.png"),
UIImage(named:"photo3.png"),
UIImage(named:"photo4.png")]
let pageCount = pageImages.count
pageControl.currentPage = 0
pageControl.numberOfPages = pageCount
for _ in 0..<pageCount {
pageViews.append(nil)
}
let pagesScrollViewSize = scrollView.frame.size
println("pagesScrollViewSize : \(pagesScrollViewSize)")
scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageCount), pagesScrollViewSize.height)
loadVisiblePages()
}
//when scrolling
func scrollViewDidScroll(scrollView: UIScrollView!){
loadVisiblePages()
}
func loadVisiblePages(){
let pageWidth = scrollView.frame.size.width
let page = Int(floor( (scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth*2.0) ))
println("page: \(page)")
pageControl.currentPage = page
let firstPage = page-1
let lastPage = page+1
//remove all the pages before firstPage
for var index = 0; index < firstPage; ++index{
purgePage(index)
}
//load pages in our range
for var index = firstPage; index <= lastPage; ++index {
loadPage(index)
}
//remove after lastPage
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}
func loadPage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
//already loaded
}
else {
var frame = scrollView.bounds
frame.origin.x = frame.size.width * CGFloat(index)
frame.origin.y = 0.0
println("\(frame)")
var newImageView = UIImageView(image:pageImages[index])
newImageView.contentMode = .ScaleAspectFit
newImageView.frame = frame
scrollView.addSubview(newImageView)
pageViews[index] = newImageView
}
}
func purgePage(index:Int){
if index < 0 || index >= pageImages.count {
return
}
if let pageView = pageViews[index] {
pageView.removeFromSuperview()
pageViews[index] = nil
}
}
}
谢谢
大小 类(或任何自动布局更改,就此而言)在 viewDidAppear
或 viewDidLayoutSubviews
之后才会应用。您需要将此代码移至其中一个。您看到的是视图的框架,因为它是从 Interface Builder 文档(xib 或故事板)加载出来的。在这些生命周期事件之后,您永远不应该检查帧。
Unable to set frame correctly before viewDidAppear