【发布时间】:2015-10-21 10:50:11
【问题描述】:
我正在制作一个应用程序,我需要 x 数量的自定义 UIView 并将它们放在 scrollView 中。 scrollView 和UIView xib 的布局是用AutoLayout 设置的。我现在得到的结果是这样的:
在此处查看我的 VC 代码。
let sponsors = Sponsors.createSponsors() <-- Array
override func viewDidLoad() {
super.viewDidLoad()
configureSponsors()
}
//MARK: Load the AddView in ScrollView
func configureSponsors() {
self.scrollView.contentSize = CGSizeMake(CGFloat(sponsors.count) * self.scrollView.frame.size.width, self.scrollView.frame.size.height)
for sponsor in sponsors {
numberOfItems++
let addView = NSBundle.mainBundle().loadNibNamed("AddView", owner: self, options: nil).last as! AddView
addView.addDataToAddView(sponsor)
addView.frame = CGRectMake(CGFloat(numberOfItems - 1) * scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height)
self.scrollView.addSubview(addView)
}
}
这是我的 UIView 代码:
//MARK: Outlets
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var sponsorTitle: UILabel!
@IBOutlet weak var sponsorLogo: UIImageView!
@IBOutlet weak var sponsorSubtitle: UILabel!
@IBOutlet weak var roundView: UIView!
@IBOutlet weak var playMusicButton: UIButton!
//MARK: Properties
var cornerRadius: CGFloat = 3.0
func addDataToAddView(sponsor: Sponsors) {
backgroundImageView.image = UIImage(named: sponsor.backgroundImage)
sponsorLogo.image = UIImage(named: sponsor.logoImage)
sponsorTitle.text = sponsor.title
sponsorSubtitle.text = sponsor.subTitle
}
override func layoutSubviews() {
roundView.layer.cornerRadius = roundView.frame.size.width / 2
roundView.alpha = 0.7
roundView.clipsToBounds = true
}
//MARK: PlayVideo
@IBAction func playVideo(sender: UIButton) {
//play music
}
我已经搜索过相同的问题。我发现我必须使用Pure Auto Layout Approach。这是否意味着我需要以编程方式设置 UIView 的约束? 非常感谢,
达克斯
更新:
scrollView 设置图片:
【问题讨论】:
-
@BadalShah 我已经尝试了建议代码来覆盖滚动视图
contentOffset,但结果相同。 -
如何以编程方式或通过情节提要设置滚动视图?你能告诉我你在故事板中的约束吗?
-
@BadalShah 通过情节提要,查看更新
标签: ios swift uiview uiscrollview autolayout