这次继续上次的内容。在上次的部分,我做了一个搜索的按钮,并且完成了其功能。搜索按钮放置在了发现页面的Navigation的右侧,但是发现页面本身却没有进行设计。这次我将对这个发现页面进行一些初步设计。在计划中,这个地方同样是放上一些项目的条目,来供用户浏览。但是,其中还涉及到项目的分类。因此这里我设计了一个UISegement,来进行分类。网上有一些相对简单的Segement的样例,但是都不尽人意。于是我自己找到了一个第三方的UISegement的开源库——JTSegmentControl。在学习使用了之后,我完成了一个动态滑动的选择栏。
具体的效果如下
代码如下
//
// SearchViewController.swift
// FinalTest
//
// Created by 沈力同 on 2017/5/10.
// Copyright © 2017年 沈力同. All rights reserved.
//
import UIKit
class SearchViewController: UIViewController {
let screenSizeWidth = UIScreen.main.bounds.size.width
let screenSizeHeight = UIScreen.main.bounds.size.height
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
//右侧搜索按钮
var img1=UIImage(named: "search")
let item=UIBarButtonItem(image:img1, style: UIBarButtonItemStyle.plain, target: self, action: #selector(LogViewController.click_register))
self.navigationItem.rightBarButtonItem=item
self.navigationController?.navigationBar.tintColor = UIColor.black
let topBackgroundView = UIView(frame:CGRect(x:0.0, y: 64, width: self.view.bounds.size.width, height: 44.0))
topBackgroundView.backgroundColor = UIColor(red: 255.0/255, green: 250.0/255, blue: 250.0/255, alpha: 1.0)
self.view.addSubview(topBackgroundView)
// //MARK - init JTSegement
// var frame = CGRect(x: 10.0, y: 64.0, width: self.view.bounds.size.width - 20.0, height: 44.0)
// let segmentControl = JTSegmentControl(frame: frame)
// segmentControl.delegate = self
// segmentControl.items = ["first", "second", "third", "fouth"]
// segmentControl.showBridge(show: true, index: 1)
// segmentControl.autoScrollWhenIndexChange = false
//MARK - init autoAdjustWidth JTSegement
var frame = CGRect(x:0.0, y: 64, width: self.view.bounds.size.width, height: 44.0)
let autoWidthControl = JTSegmentControl(frame: frame)
autoWidthControl.delegate = self
autoWidthControl.items = ["first", "second", "third", "fouth", "fifth", "sixth", "seventh", "eighth"]
// autoWidthControl.items = ["first", "second", "third", "fouth"]
autoWidthControl.showBridge(show: false, index: 4)
autoWidthControl.selectedIndex = 0
autoWidthControl.autoAdjustWidth = true
autoWidthControl.bounces = true
view.addSubview(autoWidthControl)
//MARK - test display
self.label.text = "this is index:" + String(autoWidthControl.selectedIndex)
self.label.sizeToFit()
label.center.x = self.view.center.x
label.center.y = self.view.center.y + 50.0
label.textColor = UIColor.darkGray
label.textAlignment = .center
view.addSubview(label)
let SearchDetailViewController0 = SearchDetailViewController_0()
SearchDetailViewController0.view.frame = CGRect(x:0, y:108,
width:screenSizeWidth, height:screenSizeHeight)
self.view.addSubview(SearchDetailViewController0.view)
}
func click_register(){
let SearchButtonView = SearchButtonViewController()
self.navigationController?.pushViewController(SearchButtonView, animated: true)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
// self.segmentControl?.itemTextColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension SearchViewController : JTSegmentControlDelegate {
func didSelected(segement: JTSegmentControl, index: Int) {
print("current index \(index)")
segement.showBridge(show: false, index: index)
switch index {
case 0:
let SearchDetailViewController0 = SearchDetailViewController_0()
SearchDetailViewController0.view.frame = CGRect(x:0, y:108,
width:screenSizeWidth, height:screenSizeHeight)
self.view.addSubview(SearchDetailViewController0.view)
case 1:
let SearchDetailViewController1 = SearchDetailViewController_1()
SearchDetailViewController1.view.frame = CGRect(x:0, y:108,
width:screenSizeWidth, height:screenSizeHeight)
self.view.addSubview(SearchDetailViewController1.view)
case 2:
let SearchDetailViewController2 = SearchDetailViewController_2()
SearchDetailViewController2.view.frame = CGRect(x:0, y:108,width:screenSizeWidth, height:screenSizeHeight)
self.view.addSubview(SearchDetailViewController2.view)
// case 3:
// let mainT = MainTableViewController()
// mainT.view.frame = CGRect(x:0, y:108,width:screenSizeWidth, height:screenSizeHeight)
// self.view.addSubview(mainT.view)
default:
let SearchDetailViewController0 = SearchDetailViewController_0()
SearchDetailViewController0.view.frame = CGRect(x:0, y:108,
width:screenSizeWidth, height:screenSizeHeight)
self.view.addSubview(SearchDetailViewController0.view)
}
}
}