gpt4 book ai didi

自定义工具栏中按钮的快速定位(谷歌设计 Material )

转载 作者:行者123 更新时间:2023-11-28 06:36:21 25 4
gpt4 key购买 nike

我有一个由谷歌设计 Material 框架创建的自定义工具栏。另外,我想在工具栏中实现 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)


}

最佳答案

试试这个:

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("!!!!!!")
}
}

}

结果:

enter image description here

关于自定义工具栏中按钮的快速定位(谷歌设计 Material ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38989594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com