ios - 我如何在标签栏项目上添加自定义 View
<p><p> <a href="/image/Dxt9L.png" rel="noreferrer noopener nofollow"><img src="/image/Dxt9L.png" alt="enter image description here"/></a> </p>
<p>我需要在标签栏项目上添加两条垂直线。我怎么能做到这一点?谢谢。</p>
<p>添加下一个代码:</p>
<pre><code>var imageLeft: UIImageView?
var imageRight: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
let grayColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
let leftLine = (tabBar.frame.width/2) - ((tabBar.frame.width/5)/2)
let rightLine = (tabBar.frame.width/2) + ((tabBar.frame.width/5)/2)
imageLeft = UIImageView(image: createImage(color: grayColor, size: tabBar.frame.size, x: leftLine))
imageRight = UIImageView(image: createImage(color: grayColor, size: tabBar.frame.size, x: rightLine))
tabBar.addSubview(imageLeft!)
tabBar.addSubview(imageRight!)
}
func createImage(color: UIColor, size: CGSize, x: CGFloat) -> UIImage {
let rect: CGRect = CGRect(x: x, y: 5, width: 1, height: tabBar.frame.height - 11)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
</code></pre>
<p>我的结果:</p>
<p> <a href="/image/jHbrt.png" rel="noreferrer noopener nofollow"><img src="/image/jHbrt.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>为此,您可以继承 UITabBarController 并在您想要的位置添加 UIImageView:</p>
<pre><code>class TabBarController: UITabBarController {
var image: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
image = UIImageView(image: createImage(color: UIColor(red:0.18, green:0.66, blue:0.24, alpha:1.0), size: tabBarItemSize, lineHeight: 4))
tabBar.addSubview(image!)
}
func createImage(color: UIColor, size: CGSize, lineHeight: CGFloat) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: size.height - lineHeight, width: size.width, height: lineHeight )
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
</code></pre>
<p>您需要根据需要对其进行修改并计算您希望它所在的位置,但这应该会有所帮助。我在 <a href="https://medium.com/@JillevdWeerd/bringing-animation-to-uitabbarcontroller-1b8780ced374" rel="noreferrer noopener nofollow">an article I wrote</a> 中对此有所了解.</p></p>
<p style="font-size: 20px;">关于ios - 我如何在标签栏项目上添加自定义 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49050842/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49050842/
</a>
</p>
页:
[1]