1

Swift 5.1.3、iOS13.3、XCode11.3、

シンプルなボタン ターゲット アクションを作成しようとしています。

ただし、Button は独自の StackView クラス内にあり、さらに遅延ボタンです。

コードでターゲット アクションが機能しないのはなぜですか? つまりcallButtonMethod、呼び出されることはありません。

API の応答が遅いためか、レイジー ボタンがターゲット アクションを実行できないためである可能性があります。私は現時点では無知です。

これについて助けてくれてありがとう。

これが私のコードです:

class CockpitHeaderStackView: UIStackView {

    weak var profileBtnDelegate: CallButtonProfileImage?

    var profileImageView = UIImageView()
    var profileName = "Cockpit".localized

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    private func commonInit() {

        if let profile = MyAPI.profile, let person = profile.person {
            profileName = "\(person.firstName ?? "") \(person.lastName ?? "")"
        }

        MyAPI.getPicture(PictureType.avatar) { [weak self] (error, image) in

            guard let self = self else { return } // check if self still alive otherwise bail out

            DispatchQueue.main.async {

                if let image = image {
                    self.profileImageView.image = image
                } else {
                    self.profileImageView.image = #imageLiteral(resourceName: "profile-placeholder-small")
                }
                self.profileImageView.contentMode = .scaleAspectFill

                self.axis = .horizontal
                self.alignment = .bottom
                self.spacing = 10.0
                self.addArrangedSubview(self.titleLabel)
                self.addArrangedSubview(self.button)
            }
        }
    }

    lazy var titleLabel: UILabel = {
        let labelWidth: CGFloat = UIScreen.main.bounds.width - 16.0 - 10.0 - 36.0 - 16.0   // FullScreenWidth minus (Leading + Spacing + ButtonWidth + Trailing)
        let label = UILabel()
        label.font = AppConstants.Font.NavBar_TitleFont
        label.text = profileName
        label.textColor = .white
        label.tintColor = .white
        label.widthAnchor.constraint(equalToConstant: labelWidth).isActive = true
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    lazy var button: UIButton = {

        let buttonWidth: CGFloat = 36.0
        let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: buttonWidth, height: buttonWidth)))

        button.setImage(self.profileImageView.image, for: .normal)
        button.addTarget(self, action: #selector(callButtonMethod), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
        button.layer.cornerRadius = button.frame.size.width / 2
        button.layer.masksToBounds = false
        button.clipsToBounds = true

        button.translatesAutoresizingMaskIntoConstraints = false
        button.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
        button.heightAnchor.constraint(equalToConstant: buttonWidth).isActive = true

        return button
    }()

    @objc func callButtonMethod() {
        profileBtnDelegate?.callProfileBtnMethod()
    }
}

CockpitHeaderStackViewViewController のカスタム NavigationBar を作成するために使用されます。

を使用したカスタム NavigationBar のコードは次のCockpitHeaderStackViewとおりです。

protocol CallButtonProfileImage: AnyObject {
    func callProfileBtnMethod()
}

class MyViewController: UIViewController {

    // ...

    lazy var titleStackView: CockpitHeaderStackView = {
        let titleStackView = CockpitHeaderStackView(frame: CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: 88.0)))
        titleStackView.translatesAutoresizingMaskIntoConstraints = false
        return titleStackView
    }()

    lazy var cockpitHeaderView: UIView = {
        let cockpitHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: 88.0)))
        cockpitHeaderView.addSubview(titleStackView)
        titleStackView.leadingAnchor.constraint(equalTo: cockpitHeaderView.leadingAnchor, constant: 16.0).isActive = true
        titleStackView.topAnchor.constraint(equalTo: cockpitHeaderView.topAnchor).isActive = true
        titleStackView.trailingAnchor.constraint(equalTo: cockpitHeaderView.trailingAnchor, constant: -16.0).isActive = true
        titleStackView.bottomAnchor.constraint(equalTo: cockpitHeaderView.bottomAnchor).isActive = true
        return cockpitHeaderView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // ...

        view.clipsToBounds = true

        navigationController?.set_iOS12_lookAndFeel()
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        navigationItem.largeTitleDisplayMode = .always
    }

    override func viewWillLayoutSubviews() {
        // replace NavBar title by custom cockpitHeaderView
        self.title = ""
        self.navigationItem.titleView = self.cockpitHeaderView
        // position the cockpitHeaderView inside the largeTitleDisplayMode NavBar
        self.cockpitHeaderView.translatesAutoresizingMaskIntoConstraints = false
        self.cockpitHeaderView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        if let navBarBottomAnchor = self.navigationController?.navigationBar.bottomAnchor {
            if UIScreen.main.bounds.height > 568.0 {
                self.cockpitHeaderView.topAnchor.constraint(equalTo: navBarBottomAnchor, constant: -48.0).isActive = true
            } else {
                self.cockpitHeaderView.topAnchor.constraint(equalTo: navBarBottomAnchor, constant: -46.0).isActive = true  // iPhone SE space limitation
            }
        } else {
            self.cockpitHeaderView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 89.0).isActive = true
        }
    }

    func callProfileBtnMethod() {
        print("right BarButton called here")
    }
}
4

1 に答える 1