在大标题导航栏中添加了一个按钮

时间:2019-06-10 15:44:56

标签: swift uibutton navigationbar

我在大标题导航栏中添加了一个按钮。我想要像在模型中一样使按钮的按钮宽度更宽。我用他的Medium article来作为起点。

表单模拟器

enter image description here

来自模型

the mockup

我的代码

private func setupUI() {
        navigationController?.navigationBar.prefersLargeTitles = true

        title = "Large Title"
        btnShare.setTitle("Share", for: .normal)
        // Initial setup for image for Large NavBar state since the the screen always has Large NavBar once it gets opened
        guard let navigationBar = self.navigationController?.navigationBar else { return }
        navigationBar.addSubview(btnShare)
        //btnShare.layer.cornerRadius = Const.UIButonSizeForLargeState / 2
        btnShare.clipsToBounds = true
        btnShare.translatesAutoresizingMaskIntoConstraints = false
        btnShare.backgroundColor = UIColor(red:0.00, green:0.48, blue:1.00, alpha:1.0)
        NSLayoutConstraint.activate([
            btnShare.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -Const.UIButonRightMargin),
            btnShare.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor, constant: -Const.UIButonBottomMarginForLargeState),
            btnShare.heightAnchor.constraint(equalToConstant: Const.UIButonSizeForLargeState),
            btnShare.widthAnchor.constraint(equalTo: btnShare.heightAnchor)
            ])
    }




extension TransactionsTableViewController {
    /// WARNING: Change these constants according to your project's design
    private struct Const {
        /// Image height/width for Large NavBar state
        static let UIButonSizeForLargeState: CGFloat = 50
        /// Margin from right anchor of safe area to right anchor of Image
        static let UIButonRightMargin: CGFloat = 5
        /// Margin from bottom anchor of NavBar to bottom anchor of Image for Large NavBar state
        static let UIButonBottomMarginForLargeState: CGFloat = 12
        /// Margin from bottom anchor of NavBar to bottom anchor of Image for Small NavBar state
        static let UIButonBottomMarginForSmallState: CGFloat = 6
        /// Image height/width for Small NavBar state
        static let UIButonSizeForSmallState: CGFloat = 32
        /// Height of NavBar for Small state. Usually it's just 44
        static let NavBarHeightSmallState: CGFloat = 44
        /// Height of NavBar for Large state. Usually it's just 96.5 but if you have a custom font for the title, please make sure to edit this value since it changes the height for Large state of NavBar
        static let NavBarHeightLargeState: CGFloat = 96.5
    }

0 个答案:

没有答案