我正在尝试以编程方式创建一个pinterest样式布局。我使用这个项目作为参考并尝试转换它。我试图摆脱故事板并让它以编程方式运行。
我知道我必须将代码更改为App委托和集合视图单元格。
app委托的代码是:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
}
我想我可以将代码更改为:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = PhotoStreamViewController()
return true
}
}
我认为我需要更改的另一个区域是集合视图单元格。目前的代码是:
class AnnotatedPhotoCell: UICollectionViewCell {
@IBOutlet fileprivate weak var imageView: UIImageView!
@IBOutlet fileprivate weak var imageViewHeightLayoutConstraint:
NSLayoutConstraint!
@IBOutlet fileprivate weak var captionLabel: UILabel!
@IBOutlet fileprivate weak var commentLabel: UILabel!
var photo: Photo? {
didSet {
if let photo = photo {
imageView.image = photo.image
captionLabel.text = photo.caption
commentLabel.text = photo.comment
}
}
}
override func apply(_ layoutAttributes:
UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
if let attributes = layoutAttributes as? PinterestLayoutAttributes {
imageViewHeightLayoutConstraint.constant = attributes.photoHeight
}
}
}
我认为这段代码可行,但是我没有成功启动应用程序:
class AnnotatedPhotoCell: UICollectionViewCell {
let roundedCornersView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
return view
}()
var imageView: UIImageView!
var imageViewHeightLayoutConstraint: NSLayoutConstraint!
var captionLabel: UILabel!
var commentLabel: UILabel!
var photo: Photo? {
didSet {
addSubview(roundedCornersView)
roundedCornersView.addSubview(imageView)
roundedCornersView.addSubview(captionLabel)
roundedCornersView.addSubview(commentLabel)
if let photo = photo {
imageView.image = photo.image
captionLabel.text = photo.caption
commentLabel.text = photo.comment
}
captionLabel.heightAnchor.constraint(equalToConstant: 17).isActive = true
roundedCornersView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
roundedCornersView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
roundedCornersView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
roundedCornersView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
captionLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 4).isActive = true
captionLabel.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 4).isActive = true
captionLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -4).isActive = true
commentLabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 4).isActive = true
commentLabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -4).isActive = true
commentLabel.topAnchor.constraint(equalTo: captionLabel.bottomAnchor).isActive = true
imageViewHeightLayoutConstraint = imageView.heightAnchor.constraint(equalToConstant: 120)
imageViewHeightLayoutConstraint?.isActive = true
}
}
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes!) {
super.apply(layoutAttributes)
if let attributes = layoutAttributes as? PinterestLayoutAttributes {
imageViewHeightLayoutConstraint.constant = attributes.photoHeight
}
}
}
如果你能帮我解决这个问题,我会很感激。不知道我哪里出错了。
由于