以编程方式创建可滚动下拉菜单(iOS/Swift)
单击“颜色”以编程方式创建可滚动下拉菜单(iOS/Swift),ios,swift,drop-down-menu,uiscrollview,swift3,Ios,Swift,Drop Down Menu,Uiscrollview,Swift3,单击“颜色”导航栏按钮时,应显示15种颜色的下拉菜单。下拉列表将占据屏幕高度和宽度的一半。用户可以向下滚动下拉列表以查看所有颜色。但是,根据我现在掌握的代码,单击导航栏按钮时不会发生任何事情。尽管出于测试目的,当我将滚动视图的背景色设置为更可见的颜色时,我发现滚动视图占据了屏幕宽度和高度的一半。当我在colorButtonTapped()的末尾添加view.addSubview(colorView)时,我看到下拉显示占据了屏幕宽度的一半和高度的一半 AppDelegate: func a
导航栏按钮时,应显示15种颜色的下拉菜单。下拉列表将占据屏幕高度和宽度的一半。用户可以向下滚动下拉列表以查看所有颜色。但是,根据我现在掌握的代码,单击导航栏按钮时不会发生任何事情。尽管出于测试目的,当我将滚动视图的背景色设置为更可见的颜色时,我发现滚动视图占据了屏幕宽度和高度的一半。当我在colorButtonTapped()
的末尾添加view.addSubview(colorView)
时,我看到下拉显示占据了屏幕宽度的一半和高度的一半
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: ViewController())
window?.makeKeyAndVisible()
return true
}
每次调用viewWillLayoutSubviews
时,都没有必要更新scrollView
和colorView
的帧。按下按钮即可创建
下面的代码会将colorView
的x
和y
的框架精确地设置在滚动视图的右边缘,因此它不可见。两者都应为0
,以将其定位到左侧边缘
colorView.frame = CGRect(x: UIScreen.main.bounds.width / 2.0, y: y, width: UIScreen.main.bounds.width / 2.0, height: UIScreen.main.bounds.height)
最后是colorView
的isUserInteractionEnabled
属性。默认情况下,它处于启用状态,因此它会从scrollView
接收手势识别器。将其设置为false
,然后
为了在将来克服类似的问题,我建议阅读更多关于
它工作得很好,但是button.addTarget不工作..pls你说的addTarget
是什么意思?
colorView.frame = CGRect(x: UIScreen.main.bounds.width / 2.0, y: y, width: UIScreen.main.bounds.width / 2.0, height: UIScreen.main.bounds.height)
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var colorView: UIView!
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
colorView = UIView()
scrollView = UIScrollView()
let colorButton = UIBarButtonItem(title: "Colors", style: .plain, target: self, action: #selector(colorButtonTapped))
colorButton.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14.0), NSForegroundColorAttributeName: UIColor.black], for: UIControlState())
navigationController?.navigationBar.topItem?.rightBarButtonItem = colorButton
}
func colorButtonTapped() {
let colorOptions = ["Blue", "Black", "Red", "Green", "Yellow", "Purple", "Orange", "Pink", "Magenta", "Lavender", "Beige", "Tan", "Burgundy", "Eggshell", "Brown"]
let y = (navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height
scrollView.frame = CGRect(x: UIScreen.main.bounds.width / 2.0, y: y, width: UIScreen.main.bounds.width / 2.0, height: UIScreen.main.bounds.height / 2.0)
scrollView.delegate = self
scrollView.isScrollEnabled = true
let buttonHeight: CGFloat = UIScreen.main.bounds.height / 15
let contentHeight: CGFloat = CGFloat(colorOptions.count) * buttonHeight
colorView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.width, height: contentHeight)
colorView.layer.cornerRadius = 8
colorView.clipsToBounds = true
colorView.isUserInteractionEnabled = false
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width / 2.0, height: contentHeight)
for (index, title) in colorOptions.enumerated() {
let button = UIButton(type: .custom)
button.backgroundColor = .red
button.frame = CGRect(x: 0, y: buttonHeight * CGFloat(index), width: UIScreen.main.bounds.width / 2.0, height: buttonHeight)
button.setTitle(title, for: .normal)
colorView.addSubview(button)
}
scrollView.addSubview(colorView)
view.addSubview(scrollView)
}
}