переместите кнопки панели влево на панели инструментов с помощью swift - PullRequest
0 голосов
/ 07 июня 2018

У меня есть панель инструментов, которая находится внизу (правильно).Проблема в том, что кнопка (на UIBarButtonItem) находится в центре всей панели инструментов.

Как я могу расположить эту кнопку сбоку, но по-прежнему (вертикально) по центру с изображением? Это было бы лучше с запасом.

Таким образом, это выглядело бы как

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

Код:

    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)]

Где iconBar -Панель инструментов определяется как:

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

Также я использую расширение для центрирования кнопки UIB, затем я могу добавить изображение (также по центру) над ним.Вот расширение:

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)
    }
}

1 Ответ

0 голосов
/ 07 июня 2018

Вы ищете «элемент кнопки гибкой пробел» или «элемент кнопки фиксированной пробел»?затем вы можете добавить немного места слева и справа внутри панели инструментов и определить его размер.

Это будет выглядеть примерно так:

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

spacer.width = 10
...