问题描述
限时送ChatGPT账号..我想在顶部创建一个自定义标签栏.就像9gag一样.你知道我可以学习如何做的地方吗?(文档或视频教程)谢谢9gag 自定义标签栏
I want to create a custom tab bar at the top. Just like 9gag. Do you know a place where i can learn how to do it? (documentiation or video tutorial) Thanks 9gag custom tab bar
推荐答案
我实施的一项工作.
创建一个具有自定义标签栏的视图.
Create a view to have the custom tab bar.
let tabView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60))
tabView.backgroundColor = UIColor(red: 45/255, green: 58/255, blue: 114/255, alpha: 1)
self.view.addSubview(tabView)
根据按钮的数量,决定按钮的大小并将它们添加为子视图.(此处取 2 个)
Depending on the number of buttons, decide the button size and add them as subview.(Taking 2 here)
firstButton = UIButton(type: .Custom)
firstButton.frame = CGRect(x: 0, y: 20, width: tabView.frame.size.width / 2, height: 30)
firstButton.setTitle("BUT 1", forState: .Normal)
firstButton.setTitleColor(UIColor(red: 0/255, green: 191/255, blue: 165/255, alpha: 1.0), forState: .Normal)
firstButton.addTarget(self, action: #selector(ViewController.firstButtonTapped), forControlEvents: .TouchUpInside)
tabView.addSubview(firstButton)
secondButton = UIButton(type: .Custom)
secondButton.frame = CGRect(x: tabView.frame.size.width / 2, y: 20, width: tabView.frame.size.width / 2, height: 30)
secondButton.setTitle("BUT 2", forState: .Normal)
secondButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
secondButton.addTarget(self, action: #selector(ViewController.secondButtonTapped), forControlEvents: .TouchUpInside)
tabView.addSubview(secondButton)
添加一个小的 UIView,作为按钮下方的一行.
Add a small UIView which serves as a line below the buttons.
lineView = UIView()
lineView.frame = CGRect(x: 0, y: CGRectGetMaxY(tabView.frame) - 3, width: tabView.frame.size.width / 2, height: 3)
lineView.backgroundColor = UIColor(red: 0/255, green: 191/255, blue: 165/255, alpha: 1.0)
tabView.addSubview(lineView)
在滚动视图中创建下面的视图.
Make the view below in a scrollview.
self.scrollView.frame = CGRectMake(0, CGRectGetMaxY(tabView.frame), self.view.frame.width, self.view.frame.height)
self.scrollView.backgroundColor = UIColor.whiteColor()
self.scrollView.delegate = self
self.scrollView.pagingEnabled = true
self.view.addSubview(self.scrollView)
let aScrollViewWidth = self.scrollView.frame.width
let aScrollViewHeight = self.scrollView.frame.height
self.scrollView.contentSize = CGSizeMake(aScrollViewWidth * CGFloat(2), aScrollViewHeight)
一个接一个地添加你的观点.为了这个例子,我只是添加了一个 UITextView.
Add your views one after the other. For the sake of the example, I am just adding a UITextView.
for anIndex in 0 ..< 2 {
let anEssayTextView = UITextView(frame: CGRectMake(aScrollViewWidth * CGFloat(anIndex), 0, aScrollViewWidth, aScrollViewHeight))
anEssayTextView.text = essays[anIndex]
anEssayTextView.editable = false
self.scrollView.addSubview(anEssayTextView)
}
ScrollView 委托函数
ScrollView delegate function
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
if (pageNumber == 0) {
firstButtonTapped()
} else {
secondButtonTapped()
}
}
按钮操作
Button actions
func firstButtonTapped() {
firstButton.setTitleColor(UIColor(red: 0/255, green: 191/255, blue: 165/255, alpha: 1.0), forState: .Normal)
secondButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
if (self.lineView.frame.origin.x != 0) {
UIView.animateWithDuration(0.25) {
self.lineView.frame.origin.x -= self.tabView.frame.size.width / 2
}
}
scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
pageNumber = 0
}
func secondButtonTapped() {
firstButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
secondButton.setTitleColor(UIColor(red: 0/255, green: 191/255, blue: 165/255, alpha: 1.0), forState: .Normal)
if (self.lineView.frame.origin.x != self.tabView.frame.size.width / 2) {
UIView.animateWithDuration(0.25) {
self.lineView.frame.origin.x += self.tabView.frame.size.width / 2
}
}
scrollView.setContentOffset(CGPointMake(self.scrollView.frame.size.width, 0), animated: true)
pageNumber = 1
}
这篇关于创建自定义标签栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论