Help us understand the problem. What is going on with this article?

A auto-fitting custom iOS toolbar with labelled buttons

More than 1 year has passed since last update.

In iOS 11+, it is possible to provide custom UIBarButtonItems to the Toolbar. The custom CustomToolbarButtonClass used in this example has a text label centered and directly below the button icon.

The main challenge involves fitting the toolbar to its custom items. THe solution presented requires a subclass of the UIToolbar, which automatically fits the height of its items whenever there is a layout change.

This solution is backwards compatible with iOS <11 versions.

if #available(iOS 11.0, *) {
    let button =  CustomToolbarButtonClass(
        image: image,
        label: label,
        action: #selector(onButtonClicked(sender:)
    )
}
else {
    let button = UIBarButtonItem(
        image: image,
        style: .plain,
        target: self,
        action: #selector(onItemClicked(sender:)))
}

A custom toolbarClass which handles layout changes

class ToolbarClass: UIToolbar {

    var defaultHeight: CGFloat {
        get {
            return 44.0
        }
    }

    private var maxContentHeight: CGFloat? {
        get {
            return self.items?.map({ (button) -> CGFloat in
                (button as? ToolbarButtonClass)?.height ?? 0
            }).max()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let contentHeight = self.maxContentHeight {
            var newFrame = self.frame
            newFrame.size.height = max(defaultHeight, contentHeight)
            if(self.frame != newFrame) {
                self.frame = newFrame
            }
        }

    }

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        if let contentHeight = self.maxContentHeight {
            let newHeight = max(defaultHeight, contentHeight)
            if(newHeight != size.height) {
                size.height = newHeight
            }
        }
        return size
    }
}
cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした