【问题标题】:Swift iOS UITabBar CustomizationSwift iOS UITabBar 自定义
【发布时间】:2018-10-30 06:39:01
【问题描述】:

我有一个标签栏。我改变了它的颜色,给它一个圆角半径,设置它的边框宽度和颜色,一切都很好。到目前为止,一切都在故事板中完成。

现在我想给它左右边距,因为默认情况下它会粘在屏幕的边缘。

这是标签栏的当前外观:

黑色箭头指向贴在屏幕边缘的线条。我想要这条线和边缘之间的空间。

【问题讨论】:

  • margin.. 你能分享你想要的截图吗?
  • 我希望标签栏左右的黑线和屏幕墙之间有一些空间。我没有那个屏幕截图。如果您不明白,请告诉我。
  • @Myaaoonn 请看一下

标签: ios swift storyboard uitabbar


【解决方案1】:

您可以在自定义UITabBar 中创建此占位符视图,如下所示,

class CustomTabBar: UITabBar {

    let roundedView = UIView(frame: .zero)

    override func awakeFromNib() {
        super.awakeFromNib()

        roundedView.layer.masksToBounds = true
        roundedView.layer.cornerRadius = 12.0
        roundedView.layer.borderWidth = 2.0
        roundedView.isUserInteractionEnabled = false
        roundedView.layer.borderColor = UIColor.black.cgColor
        self.addSubview(roundedView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let margin: CGFloat = 12.0
        let position = CGPoint(x: margin, y: 0)
        let size = CGSize(width: self.frame.width - margin * 2, height: self.frame.height)
        roundedView.frame = CGRect(origin: position, size: size)
    }
}

storyboard/xib 中为 TabBar 设置此类。它应该给你以下,

【讨论】:

  • 完美!非常感谢
  • @HafizShoaibAwan 很高兴它有帮助!
  • @Kamran 你觉得如何,可以将标签栏移得更高吗?
  • 它解决了问题,但是现在按标签不起作用,我卡在主页标签上,您能帮帮我吗?
  • CustomTabBarController 中,覆盖viewDidLayoutSubviews 并将newFrame 设置为tabBar。新框架将具有相同的 xwidthheight,但您要更改自定义 y(例如 tabBar.frame.origin.y - 20)。
猜你喜欢
  • 2013-09-15
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
相关资源
最近更新 更多