У меня есть панель инструментов, которая размещена внизу (правильно). Проблема в том, что кнопка (на 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
}()
Также я использую расширение для центрирования UIButton, затем я могу добавить изображение (также по центру) над ним. Вот расширение:
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)
}
}
var barGap = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
, затем 'barGap.width = 10`, затем добавил его в свой toolbar.items (iconBar.items = [barGap, UIBarButtonIte
...), и это сработало. Спасибо - person Yuma Technical Inc.   schedule 07.06.2018