在 Swift 中将视觉格式语言与 iCarousel 结合使用
Using Visual Format Language with iCarousel in Swift
我喜欢在我的 swift 项目中使用 iCarousel,但有一件事我无法克服;我想在我的项目中使用可视化语言来布局视图,但是每当我使用 iCarousel 的可视化格式时,它都不起作用。
我注意到问题出在 TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
属性上。
每当我禁用此属性时,iCarousel 都会禁用我的视觉格式约束,而每当我启用它时,约束都可以正常工作,但我的 iCarousel 不会滚动并始终保持静止。
当前代码:
#
import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
TopMenuCarousel = iCarousel(frame: CGRect())
view.addSubview(TopMenuCarousel)
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
let views = [ "TopMenuCarousel": TopMenuCarousel ]
// 2
var allConstraints = [NSLayoutConstraint]()
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde")
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 20, height: 20))
tempView.backgroundColor = UIColor.blueColor()
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
#
您似乎没有为 iCarousel 对象设置任何高度。尝试将您的第一个约束更改为:
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(20)]",
options: [],
metrics: nil,
views: views)
这是原始代码的完整修改版本。我放大了视图(你的是 20x20),并添加了一些颜色以便更容易看到发生了什么。
import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
// array of colors to make it easy to see the individual Carousel views
let arrayOfColors = [ UIColor.blueColor(), UIColor.redColor(), UIColor.yellowColor(), UIColor.orangeColor(), UIColor.greenColor()]
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
self.view.backgroundColor = UIColor.lightGrayColor()
// initialize the TopMenuCarousel object
TopMenuCarousel = iCarousel(frame: CGRect())
// add TopMenuCarousel to the view
view.addSubview(TopMenuCarousel)
// if clipsToBounds == true, TopMenuCarousel subviews will be clipped to the TopMenuCarousel frame
// default is false
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
TopMenuCarousel.backgroundColor = UIColor.purpleColor()
let views = [ "TopMenuCarousel": TopMenuCarousel ]
var allConstraints = [NSLayoutConstraint]()
// position TopMenuCarousel 100 from the Top, with a Height of 200
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(200)]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
// set TopMenuCarousel to stretch the full Width of the view
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
// this property *must* be set to false
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number \(TopMenuCarouselCount)")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde", index)
// create a 200 x 160 view to add to TopMenuCarousel
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 200, height: 160))
// give it one of the colors
tempView.backgroundColor = arrayOfColors[index % arrayOfColors.count]
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
约束已创建并激活但从未添加到视图中,您应该使用:
view.addConstraints(allConstraints)
之后
NSLayoutConstraint.activateConstraints(allConstraints)
此外,正如 DonMag 所说,没有高度限制,这意味着如果 iCarousel 视图没有正确的固有尺寸,它将不会显示,您应该像 DonMag 写的示例一样添加明确的高度。
我喜欢在我的 swift 项目中使用 iCarousel,但有一件事我无法克服;我想在我的项目中使用可视化语言来布局视图,但是每当我使用 iCarousel 的可视化格式时,它都不起作用。
我注意到问题出在 TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
属性上。
每当我禁用此属性时,iCarousel 都会禁用我的视觉格式约束,而每当我启用它时,约束都可以正常工作,但我的 iCarousel 不会滚动并始终保持静止。
当前代码:
#import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
TopMenuCarousel = iCarousel(frame: CGRect())
view.addSubview(TopMenuCarousel)
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
let views = [ "TopMenuCarousel": TopMenuCarousel ]
// 2
var allConstraints = [NSLayoutConstraint]()
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde")
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 20, height: 20))
tempView.backgroundColor = UIColor.blueColor()
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
#
您似乎没有为 iCarousel 对象设置任何高度。尝试将您的第一个约束更改为:
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(20)]",
options: [],
metrics: nil,
views: views)
这是原始代码的完整修改版本。我放大了视图(你的是 20x20),并添加了一些颜色以便更容易看到发生了什么。
import UIKit
import iCarousel
class Step2_HomePage: UIViewController,iCarouselDelegate,iCarouselDataSource {
// array of colors to make it easy to see the individual Carousel views
let arrayOfColors = [ UIColor.blueColor(), UIColor.redColor(), UIColor.yellowColor(), UIColor.orangeColor(), UIColor.greenColor()]
let TopMenuCarouselCount = 5
var TopMenuCarousel = iCarousel()
override func viewDidLoad() {
super.viewDidLoad()
print("Step2HomePage icinde")
self.view.backgroundColor = UIColor.lightGrayColor()
// initialize the TopMenuCarousel object
TopMenuCarousel = iCarousel(frame: CGRect())
// add TopMenuCarousel to the view
view.addSubview(TopMenuCarousel)
// if clipsToBounds == true, TopMenuCarousel subviews will be clipped to the TopMenuCarousel frame
// default is false
// TopMenuCarousel.clipsToBounds = true
TopMenuCarousel.type = .Linear
TopMenuCarousel.dataSource = self
TopMenuCarousel.delegate = self
TopMenuCarousel.backgroundColor = UIColor.purpleColor()
let views = [ "TopMenuCarousel": TopMenuCarousel ]
var allConstraints = [NSLayoutConstraint]()
// position TopMenuCarousel 100 from the Top, with a Height of 200
let TopMenuCarouselTop = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-100-[TopMenuCarousel(200)]",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselTop
// set TopMenuCarousel to stretch the full Width of the view
let TopMenuCarouselHorizontal = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-0-[TopMenuCarousel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += TopMenuCarouselHorizontal
// this property *must* be set to false
TopMenuCarousel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activateConstraints(allConstraints)
}
func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
print("carousel number \(TopMenuCarouselCount)")
return TopMenuCarouselCount
}
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
print("carousel view icinde", index)
// create a 200 x 160 view to add to TopMenuCarousel
let tempView = UIView(frame: CGRect(x: 0, y: 0 , width: 200, height: 160))
// give it one of the colors
tempView.backgroundColor = arrayOfColors[index % arrayOfColors.count]
return tempView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
约束已创建并激活但从未添加到视图中,您应该使用:
view.addConstraints(allConstraints)
之后
NSLayoutConstraint.activateConstraints(allConstraints)
此外,正如 DonMag 所说,没有高度限制,这意味着如果 iCarousel 视图没有正确的固有尺寸,它将不会显示,您应该像 DonMag 写的示例一样添加明确的高度。