我有一个由Google设计素材框架创建的自定义工具栏。另外,我想在工具栏中实现3个按钮(左,中,右)。但是,我只能找到.rightControl
和.leftControl
。它只能将按钮放在右侧或左侧。 我想在中心实施一个。我怎么能以编程方式做到这一点?
private var toolbar: Toolbar!
private func prepareToolbar() {
toolbar = Toolbar()
toolbar.width = 400
toolbar.height = 80.0
let image: UIImage? = MaterialIcon.cm.settings
let logoBtn = UIImage(named: "buttonlogo") as UIImage?
// Menu button.
let menuButton: IconButton = IconButton()
menuButton.pulseColor = MaterialColor.white
menuButton.tintColor = MaterialColor.white
menuButton.setImage(image, forState: .Normal)
menuButton.setImage(image, forState: .Highlighted)
let logoButton: IconButton = IconButton()
logoButton.setImage(logoBtn, forState: .Normal)
let searchButton: IconButton = IconButton()
searchButton.pulseColor = MaterialColor.white
searchButton.tintColor = MaterialColor.white
searchButton.setImage(image, forState: .Normal)
searchButton.setImage(image, forState: .Highlighted)
toolbar.rightControls = [logoButton]
//toolbar.rightControls = [searchButton] <---------
toolbar.leftControls = [menuButton]
view.addSubview(toolbar)
}
答案 0 :(得分:1)
试试这个:
ViewController.swift
import UIKit
import Material
class ViewController: UIViewController {
/// Reference for containerView.
private var containerView: UIView!
/// Reference for Toolbar.
private var toolbar: Toolbar!
let addButton: IconButton = IconButton()
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
prepareContainerView()
prepareToolbar()
}
/// General preparation statements.
private func prepareView() {
view.backgroundColor = MaterialColor.white
}
/// Prepares the containerView.
private func prepareContainerView() {
containerView = UIView()
view.layout(containerView).edges(top: 100, left: 20, right: 20)
}
/// Prepares the toolbar
private func prepareToolbar() {
toolbar = Toolbar()
containerView.addSubview(toolbar)
// Title label.
toolbar.title = " "
toolbar.titleLabel.textColor = MaterialColor.white
toolbar.titleLabel.textAlignment = .Center
// Detail label.
//toolbar.detail = "Build Beautiful Software"
//toolbar.detailLabel.textColor = MaterialColor.white
var image: UIImage? = MaterialIcon.cm.menu
// Menu button.
let menuButton: IconButton = IconButton()
menuButton.pulseColor = MaterialColor.white
menuButton.tintColor = MaterialColor.white
menuButton.setImage(image, forState: .Normal)
menuButton.setImage(image, forState: .Highlighted)
// Add button
image = MaterialIcon.cm.add
addButton.pulseColor = MaterialColor.white
addButton.tintColor = MaterialColor.white
addButton.setImage(image, forState: .Normal)
addButton.setImage(image, forState: .Highlighted)
addButton.center = toolbar.center
let space = IconButton()
space.userInteractionEnabled = false
//spacd.pulseColor = UIColor
// Search button.
image = MaterialIcon.cm.search
let searchButton: IconButton = IconButton()
searchButton.pulseColor = MaterialColor.white
searchButton.tintColor = MaterialColor.white
searchButton.setImage(image, forState: .Normal)
searchButton.setImage(image, forState: .Highlighted)
/*
To lighten the status bar - add the
"View controller-based status bar appearance = NO"
to your info.plist file and set the following property.
*/
toolbar.backgroundColor = MaterialColor.blue.base
toolbar.leftControls = [menuButton]
toolbar.rightControls = [searchButton]
toolbar.addSubview(addButton)
toolbar.layout(addButton).center()
let gesture = UITapGestureRecognizer(target: self, action: #selector(toolBarTapGestureRecognizer))
toolbar.addGestureRecognizer(gesture)
}
func toolBarTapGestureRecognizer(tapGestureRecognizer: UITapGestureRecognizer) {
if (CGRectContainsPoint(addButton.bounds, tapGestureRecognizer.locationInView(addButton))) {
addButton.pulse()
NSLog("!!!!!!")
}
}
}
<强>结果:强>