0
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
    }()

creating a friends button

    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
    }()

creating a circle button

    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
    }()

creating a profile button

    let stackView = UIStackView(arrangedSubviews: [profileButton, circleButton, profileButton])}

The last line is the line that gives me the error: cannot use instance member 'profileButton within property initializer

Artyrcheek
  • 23
  • 5

1 Answers1

2

The important part of the error message you got is the second part which unfortunately you omitted. It is property initializer is initialized before self.


The next error you'll get after rewrite without property initalizer pattern would be Expected declaration. That means the code segment need to be inside a function. Like viewDidLoad() for example.

Thus, for it to work, your code need to be modified to something similar to:

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
    }
}
Mikasa
  • 328
  • 2
  • 7
  • After you confirm that it works. Please accept the answer, so I get the 15 points. If you have other question, please write a comment. – Mikasa May 31 '17 at 02:23