shift bar buttons to left side in toolbar with swift

Yuma Technical Inc.

I have a toolbar that is placed at the bottom (correctly). The problem is the button (on a UIBarButtonItem) sits in the center of the entire toolbar.

How can I position this button to the side, but still (vertically) centered with the image? It'd be best with a margin.

So it'd look like

 --------------
|.    X
|.  label
 --------------

The code is:

    let customButton: UIButton = UIButton(type: .custom)
    customButton.setImage(UIImage(named: "start"), for: .normal)
    customButton.setTitle("Start", for: .normal)
    customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
    customButton.setTitleColor(UIColor.white, for: .normal)
    customButton.sizeToFit()
    customButton.centerLabelVerticallyWithPadding(spacing: 5)
    customButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(startButtonAction(_:))))
    iconBar.items = [UIBarButtonItem(customView: customButton)]

Where iconBar - the toolbar is defined as:

let iconBar: UIToolbar =
{
    let view = UIToolbar()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.barTintColor = UIColor.black
    return view
}()

Also I'm using an extension to center the UIButton, then I can add an image (also centered) above it. Here's the extension:

extension UIButton
{
    func centerLabelVerticallyWithPadding(spacing:CGFloat)
    {
        // update positioning of image and title
        let imageSize = self.imageView!.frame.size
        self.titleEdgeInsets = UIEdgeInsets(top:0,
                                            left:-imageSize.width,
                                            bottom:-(imageSize.height + spacing),
                                            right:0)
        let titleSize = self.titleLabel!.frame.size
        self.imageEdgeInsets = UIEdgeInsets(top:-(titleSize.height + spacing),
                                            left:0,
                                            bottom: 0,
                                            right:-titleSize.width)

        // reset contentInset, so intrinsicContentSize() is still accurate
        let trueContentSize = self.titleLabel!.frame.union(self.imageView!.frame).size
        let oldContentSize = self.intrinsicContentSize
        let heightDelta = trueContentSize.height - oldContentSize.height
        let widthDelta = trueContentSize.width - oldContentSize.width
        self.contentEdgeInsets = UIEdgeInsets(top:heightDelta/2.0,
                                              left:widthDelta/2.0,
                                              bottom:heightDelta/2.0,
                                              right:widthDelta/2.0)
    }
}
Brooks DuBois

Are you looking for the 'flexible space bar button item' or 'fixed space bar button item'? then you can add a little bit of space to the left and right inside the toolbar and define it's size.

It will look something like this:

let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)

spacer.width = 10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nav bar with toolbar buttons on a specific tabview item in swift

Is it possible to place two buttons on left side of navigation bar with transformation of most left button on OnClick?

Center bar buttons in toolbar in Xcode

Visual Studio Code. why are the window control buttons over the menu bar on the left side?

How to position text on left side of nav bar without effecting buttons on right?

Adding buttons to toolbar programmatically in swift

Swift: No reaction from toolbar buttons

How do I move "Application menu" within the widows title bar on the left side while "minimize, maximize, close" buttons are placed on the right side?

Discount coupons in left side bar

Toolbar with custom layout: empty space on left side

How to restore the toolbar in the left side of ubuntu unity?

iCarousel - How to shift the focus to the left side

How to shift the XtickLabels of a bar graph to the left?

How to set buttons in left and right(leading/trailing) in navigation bar swift ui?

Align radio buttons on left side of the page

Left Side Navigation in Swift with UIPanGestureRecognizer

Gaps at the top and side of navigation bar buttons

Side bar buttons not wrapping to next line

Create nav bar with title on left and buttons on right

Activate the menu on the left side bar based on url

Add image to left side of action bar ?

Adding a title to the left side of the navigation bar

how to align notebook bar on left side in pygtk?

How to move Top bar to left side in css?

How to put the icons on the left side of a nav bar?

What does shift left (<<) actually do in Swift?

How to add a button on the left side of android app toolbar?

how to align toolbar title text to left side using xamarian forms

How do I add a button to the left side of the toolbar?