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

时间:2017-05-31 00:31:29

标签: ios swift swift3 uistackview

console.log(sid);

创建朋友按钮

_id

创建一个圆形按钮

console.log(ob);

创建个人资料按钮

unidentified

最后一行是给出错误的行:不能在属性初始值设定项中使用实例成员'profileButton

1 个答案:

答案 0 :(得分:2)

你得到的错误信息的重要部分是第二部分,遗憾的是你省略了。它是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
    }
}