iOS开发——UI篇Swift篇&UIToolbar

 

 

UIToolbar

 

  1 class UIToolBarUISearchBar: UIViewController,UISearchBarDelegate {

  2     

  3     var titleString:String!

  4     

  5     override func viewDidLoad() {

  6         super.viewDidLoad()

  7         

  8         // Do any additional setup after loading the view.

  9         

 10         

 11         /**********   UIToolbar  ************************************************/

 12         //创建UIToolbar实例

 13         var toolBar:UIToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 65))

 14         

 15         //添加到视图上

 16         self.view.addSubview(toolBar)

 17         

 18         //设置工具栏样式

 19         toolBar.barStyle = UIBarStyle.Default

 20         

 21         //设置工具栏按钮文字颜色

 22         toolBar.tintColor = UIColor.greenColor()

 23         

 24         //设置工具栏颜色

 25         toolBar.backgroundColor = UIColor.redColor()

 26         

 27         //创建一个间距

 28         var flexibleSpace = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.FlexibleSpace, target:"barButtonItemClicked:", action:nil)

 29         

 30         //创建一个返回按钮

 31         var backItem = UIBarButtonItem(title: "返回", style:UIBarButtonItemStyle.Plain, target:self, action:"backItemClicked:")

 32         

 33         //继续创建按钮

 34         var barBtnItem1 = UIBarButtonItem(title: "功能1", style:UIBarButtonItemStyle.Plain, target:self, action:"barBtnItemClicked:")

 35         var barBtnItem2 = UIBarButtonItem(title: "功能2", style:UIBarButtonItemStyle.Plain, target:self, action:"barBtnItemClicked:")

 36         

 37         toolBar.items = [flexibleSpace, backItem, flexibleSpace, barBtnItem1, flexibleSpace, barBtnItem2,flexibleSpace]

 38         

 39 

 40         

 41         

 42         

 43         

 44         

 45         

 46         

 47         

 48         

 49         

 50           /**********   UIToolbar  ************************************************/

 51         

 52         var searchBar = UISearchBar(frame: CGRectMake(0, 100, UIScreen.mainScreen().bounds.size.width, 45))

 53         

 54         self.view.addSubview(searchBar)

 55         

 56         //设置UISearchBar样式

 57         searchBar.barStyle = UIBarStyle.Default

 58         

 59         //设置UISearchBar的委托

 60         searchBar.delegate = self

 61         

 62         //设置UISearchBar上面的显示的文字

 63         searchBar.text = "租房点评"

 64         

 65         //设置在顶部的单行文字,通常作为一个提示行

 66         searchBar.prompt = "请关注下面App,各大市场均有下载"

 67         

 68         //搜索框占位符号-半透明的提示文字,输入搜索内容消失

 69         searchBar.placeholder = "请输入App名称"

 70         

 71         //控件右端是否显示一个书的按钮

 72         searchBar.showsBookmarkButton = true

 73         

 74        //控件右端是否显示cancel按钮

 75         searchBar.showsCancelButton = true

 76         

 77         //控件右端是否显示搜索结果按钮

 78         searchBar.showsSearchResultsButton = true

 79         

 80         //搜索结果按钮是否被选中

 81         searchBar.searchResultsButtonSelected = true

 82         

 83         //bar的颜色

 84         searchBar.tintColor = UIColor.redColor()

 85         

 86         //指定UISearchBar是否会有透视效果

 87         searchBar.translucent = true

 88         

 89         //设置在什么的情况下自动大写

 90         searchBar.autocapitalizationType = UITextAutocapitalizationType.AllCharacters

 91         

 92         //对于文本对象自动校正风格

 93         searchBar.autocorrectionType = UITextAutocorrectionType.Default

 94         

 95         //键盘的样式

 96         searchBar.keyboardType = UIKeyboardType.Default

 97         

 98         //UISearchBar下部的选择栏,数组里面的内容是按钮的标题

 99         searchBar.scopeButtonTitles = ["1","2"]

100         

101         //UISearchBar下部索引范围按钮的标题阵列。默认值是0。如果超出范围的忽视

102         searchBar.selectedScopeButtonIndex = 2

103         

104         //控制UISearchBar下部的选择栏是否显示出来

105         searchBar.showsScopeBar = true

106         

107     }

108 

109     override func didReceiveMemoryWarning() {

110         super.didReceiveMemoryWarning()

111         // Dispose of any resources that can be recreated.

112     }

113     

114 

115     /*

116     // MARK: - Navigation

117 

118     // In a storyboard-based application, you will often want to do a little preparation before navigation

119     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

120         // Get the new view controller using segue.destinationViewController.

121         // Pass the selected object to the new view controller.

122     }

123     */

124     

125     //返回按钮事件

126     func backItemClicked(sender: UIBarButtonItem)

127     {

128         self.navigationController?.popViewControllerAnimated(true)

129     }

130     

131     //

132     func barBtnItemClicked(sender: UIBarButtonItem)

133     {

134         var barItemTitle = sender.title

135         

136         var aler:UIAlertView = UIAlertView(title: "您单击的是", message: barItemTitle, delegate: nil, cancelButtonTitle: "确定")

137         aler.show()

138     }

139     

140 

141     

142      // MARK: - UISearchBarDelegate

143     

144     //搜索框开始时是否能编辑

145     func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool{

146         return true //返回允许不允许编辑

147     }

148     

149     //搜索框开始输入内容

150     func searchBarTextDidBeginEditing(searchBar: UISearchBar){

151     

152     }

153     //搜索框结束时是否能编辑

154     func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool{

155         return true

156     }

157     

158     //结束文本输入

159     func searchBarTextDidEndEditing(searchBar: UISearchBar){

160     

161     }

162     

163     //更具searchText进行搜索

164     func searchBar(searchBar: UISearchBar, textDidChange searchText: String){

165     

166     }

167     

168     //文本改变时触发

169     func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

170         return true

171     }

172     

173     //右侧搜索按钮事件

174     func searchBarSearchButtonClicked(searchBar: UISearchBar){

175     

176     }

177     

178     //右侧类似书的按钮事件

179     func searchBarBookmarkButtonClicked(searchBar: UISearchBar){

180     

181     }

182     

183     //右侧取消按钮事件

184     func searchBarCancelButtonClicked(searchBar: UISearchBar){

185     

186     }

187     

188     //右侧结果列表按钮事件

189     func searchBarResultsListButtonClicked(searchBar: UISearchBar){

190     

191     }

192     

193     //UISearchBar下面栏目选择改变触发事件

194     func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int){

195         

196     }

 

 

你可能感兴趣的:(toolbar)