美文网首页
swift--TabBar上自定义按钮,以及出现的坑

swift--TabBar上自定义按钮,以及出现的坑

作者: 码农冰冰 | 来源:发表于2016-11-21 13:57 被阅读713次
A4D71D47-1C38-4409-A785-E44C3BED860B.png

代码实现

代码的核心就是计算按钮的位置

     let cout = CGFloat(childViewControllers.count)  
        let width = tabBar.bounds.width/cout
        composeButton.frame=tabBar.bounds.insetBy(dx: 2*width, dy:-20)
        composeButton.layer.cornerRadius=width/5
        composeButton.clipsToBounds=true
        tabBar.addSubview(composeButton)

这样会导致点击按钮和消息之间出现空白,原因就是容错点的出现。

容错点:两个按钮之间会有间隙,用手点击不会出错,但是鼠标比较精确。

解决方法:把宽度减1,// 将向内缩进的宽度减少,能够让按钮的宽度变大

     let cout = CGFloat(childViewControllers.count)
        let width = tabBar.bounds.width/cout-1
        composeButton.frame=tabBar.bounds.insetBy(dx: 2*width, dy:-20)
        composeButton.layer.cornerRadius=width/5
        composeButton.clipsToBounds=true
        tabBar.addSubview(composeButton)

这里多说一句,tabBar.bounds.insetBy(dx: 2*width, dy:-20)这里类似与OC的cgrectInsert,正数向内缩进,负数向外扩展。

相关文章

网友评论

      本文标题:swift--TabBar上自定义按钮,以及出现的坑

      本文链接:https://www.haomeiwen.com/subject/tsdupttx.html