我正在尝试将相同的导航栏项添加到我的应用程序中的每个选项卡.我目前在homeController中正确设置它们,但我想将代码移动到一个单独的文件中,并在任何我想要的地方远程实现它.
例如:在导航栏的左侧添加[搜索]图标,而不必在每个swift文件中使用相同的代码.
我不知道是否应该创建一个枚举,协议或类.做这个的最好方式是什么?
let menuButton = UIButton(type: UIButtonType.system)
menuButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
menuButton.addTarget(self, action: #selector(openSearch), for: .touchUpInside)
menuButton.setImage(UIImage(named: "icon_search"), for: UIControlState())
let menuBarButtonItem = UIBarButtonItem(customView: menuButton)
navigationItem.leftBarButtonItems = [menuBarButtonItem]
func openSearch() {
// Some code
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个搜索栏和一个范围栏,但是当页面启动时,范围按钮在搜索栏后面直接可见。我知道 .Minimal 设置使我的搜索栏半透明,但它给了我一个漂亮的灰色,在白色背景上看起来很棒。
单击搜索栏,然后单击取消按钮将正确显示所有内容。下面的链接准确地显示了我在说什么。
UISearchBarStyleMinimal 在 UISearchBar 顶部显示范围按钮
有谁知道如何解决这一问题?
SearchController.swift
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
// Create search bar
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.searchBar.sizeToFit()
self.definesPresentationContext = true
self.tableView.tableHeaderView = searchController.searchBar
self.searchController.searchBar.delegate = self
self.searchController.searchBar.scopeButtonTitles = ["All", "btn1", "btn2", "btn3"]
tableView.allowsMultipleSelectionDuringEditing = true
}
Run Code Online (Sandbox Code Playgroud)
AppDelegate.swift
UISearchBar.appearance().searchBarStyle = .Minimal
UISearchBar.appearance().backgroundColor = UIColor.whiteColor()
UISearchBar.appearance().barTintColor = UIColor.whiteColor()
UISearchBar.appearance().tintColor = Constants.MAIN_THEME_COLOR
Run Code Online (Sandbox Code Playgroud) 我试图给我的控制器两个分组的tableview部分.截至目前,第一部分及其标题显示正确,但第二部分无处可寻.这两个部分都非常简单和简短,所以它们应该完全适合我的框架.
第二组在哪里,我怎样才能让它出现在第一组之下?
import UIKit
struct SectionData {
let title: String
let data: [String]
var numberOfItems: Int {
return data.count
}
subscript(index: Int) -> String {
return data[index]
}
}
extension SectionData {
init(title: String, data: String...) {
self.title = title
self.data = data
}
}
class SettingsController: UITableViewController {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.isScrollEnabled = false
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
self.tableView.backgroundColor = UIColor(white: 0.95, alpha: 1)
self.tableView.rowHeight = 60 …Run Code Online (Sandbox Code Playgroud) 我有一个包含五个项目的标签栏,我试图添加一个功能,当用户再次点击标签栏项目时滚动到顶部。添加UITabBarControllerDelegate到我想触发事件的视图中,并创建了一个函数来确定选定的标签栏索引。
当我打开应用程序时,索引 0 是自动选择的并且运行良好。当我向下滚动并点击标签栏索引时,视图会自动滚动到顶部。当我转到索引 1 并在那里触发滚动时会出现问题。它以某种方式从我的第一个标签栏项目中完全删除了自动滚动。
在没有自动滚动的情况下选择其他标签栏项目根本不会影响索引 0。
主页(索引 0)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 0 {
self.collectionView?.setContentOffset(CGPoint(x: 0, y: -10), animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
用户(索引 1)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 1 {
self.tableView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)