在Xcode中以编程方式约束侧面导航栏

时间:2019-03-11 12:26:54

标签: ios swift xcode

我尝试了几种方法来向侧面导航栏添加约束,但是我无法做到这一点。所以我做到了:

func configureNavigationBar() {
    navigationController?.navigationBar.barTintColor = .darkGray
    navigationController?.navigationBar.barStyle = .black

    navigationItem.title = "..."
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "SideBar").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleMenuToggle))
}

那是配置导航栏的代码。我想在图像左侧添加约束。我对下面的侧边栏菜单项进行了此操作。

addSubview(iconImageView)
    iconImageView.translatesAutoresizingMaskIntoConstraints = false
    iconImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    iconImageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 12).isActive = true
    iconImageView.heightAnchor.constraint(equalToConstant: 24).isActive = true
    iconImageView.widthAnchor.constraint(equalToConstant: 24).isActive = true

    addSubview(descriptionLabel)
    descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
    descriptionLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    descriptionLabel.leftAnchor.constraint(equalTo: iconImageView.rightAnchor, constant: 12).isActive = true

我也希望能够调整导航栏的大小,并在其中添加标签。

0 个答案:

没有答案