为什么我不能 return 对象数组到 Swift 中的 UIStackView?
Why can't I return an array of objects to UIStackView in Swift?
import UIKit
class MenuBar: UIView{
let buttonWidth = 50
let friendsButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Friends Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建好友按钮
let circleButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Small Circle Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建圆形按钮
let profileButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Profile Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建个人资料按钮
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])}
最后一行是给我错误的那一行:无法在 属性 初始值设定项
中使用实例成员 'profileButton
您收到的错误消息的重要部分是第二部分,不幸的是您忽略了它。是property initializer is initialized before self
。
在没有 属性 初始化器模式的情况下重写后的下一个错误将是 Expected declaration
。这意味着 code segment
需要在函数内部。例如 viewDidLoad()
。
因此,要使其正常工作,您的代码需要修改为类似于以下内容:
import UIKit
class MenuBar: UIView {
func createStackView() -> UIStackView {
let buttonWidth = 50
let friendsButton = UIImageView()
friendsButton.translatesAutoresizingMaskIntoConstraints = false
friendsButton.image = #imageLiteral(resourceName: "Friends Button")
friendsButton.contentMode = .scaleAspectFill
friendsButton.widthAnchor.constraint(equalToConstant: 50)
friendsButton.heightAnchor.constraint(equalToConstant: 50)
let circleButton = UIImageView()
circleButton.translatesAutoresizingMaskIntoConstraints = false
circleButton.image = #imageLiteral(resourceName: "Small Circle Button")
circleButton.contentMode = .scaleAspectFill
circleButton.widthAnchor.constraint(equalToConstant: 50)
circleButton.heightAnchor.constraint(equalToConstant: 50)
let profileButton = UIImageView()
profileButton.translatesAutoresizingMaskIntoConstraints = false
profileButton.image = #imageLiteral(resourceName: "Profile Button")
profileButton.contentMode = .scaleAspectFill
profileButton.widthAnchor.constraint(equalToConstant: 50)
profileButton.heightAnchor.constraint(equalToConstant: 50)
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])
return stackView
}
}
import UIKit
class MenuBar: UIView{
let buttonWidth = 50
let friendsButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Friends Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建好友按钮
let circleButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Small Circle Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建圆形按钮
let profileButton: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = #imageLiteral(resourceName: "Profile Button")
imageView.contentMode = .scaleAspectFill
imageView.widthAnchor.constraint(equalToConstant: 50)
imageView.heightAnchor.constraint(equalToConstant: 50)
return imageView
}()
创建个人资料按钮
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])}
最后一行是给我错误的那一行:无法在 属性 初始值设定项
中使用实例成员 'profileButton您收到的错误消息的重要部分是第二部分,不幸的是您忽略了它。是property initializer is initialized before self
。
在没有 属性 初始化器模式的情况下重写后的下一个错误将是 Expected declaration
。这意味着 code segment
需要在函数内部。例如 viewDidLoad()
。
因此,要使其正常工作,您的代码需要修改为类似于以下内容:
import UIKit
class MenuBar: UIView {
func createStackView() -> UIStackView {
let buttonWidth = 50
let friendsButton = UIImageView()
friendsButton.translatesAutoresizingMaskIntoConstraints = false
friendsButton.image = #imageLiteral(resourceName: "Friends Button")
friendsButton.contentMode = .scaleAspectFill
friendsButton.widthAnchor.constraint(equalToConstant: 50)
friendsButton.heightAnchor.constraint(equalToConstant: 50)
let circleButton = UIImageView()
circleButton.translatesAutoresizingMaskIntoConstraints = false
circleButton.image = #imageLiteral(resourceName: "Small Circle Button")
circleButton.contentMode = .scaleAspectFill
circleButton.widthAnchor.constraint(equalToConstant: 50)
circleButton.heightAnchor.constraint(equalToConstant: 50)
let profileButton = UIImageView()
profileButton.translatesAutoresizingMaskIntoConstraints = false
profileButton.image = #imageLiteral(resourceName: "Profile Button")
profileButton.contentMode = .scaleAspectFill
profileButton.widthAnchor.constraint(equalToConstant: 50)
profileButton.heightAnchor.constraint(equalToConstant: 50)
let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])
return stackView
}
}