为什么我不能在Swift中将对象数组返回给UIStackView

为什么我不能在Swift中将对象数组返回给UIStackView

This question already has answers here:
How to initialize properties that depend on each other
(4个答案)
两年前关闭。
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
在没有属性initalizer模式的情况下重写之后,下一个错误将是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
    }
}

关于ios - 为什么我不能在Swift中将对象数组返回给UIStackView? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44273892/

10-10 11:52