ios - Swift iOS UITabBar 自定义

标签 ios swift storyboard uitabbar

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

现在我想给它设置左右边距,因为默认情况下它贴在屏幕边缘。

这是标签栏的当前外观:

enter image description here

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

最佳答案

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

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设置这个类。它应该为您提供以下内容,

enter image description here

关于ios - Swift iOS UITabBar 自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53058677/

相关文章:

iphone - Facebook Invite friends,在我的 iOS 应用程序中..我的代码有什么错误?

ios - 不常见的png文件iOS显示

swift - 如何使用 URLSession 在异步 api 调用中查找哪个响应属于哪个请求

ios - appdelegate.m 中 View Controller 的 ivar

ios - 在 Storyboard中不使用导航 Controller 的情况下在 UIViewController 之间切换的最佳方法是什么?

ios - 如何在单击按钮时在 i-Phone 中添加弹出菜单?

ios - 减少解析 Collection View 中的加载时间

swift - 类内的变异函数

swift - TCP 套接字流和 SSL 与 Swift

ios - 找不到名为 Test.storyboard 的(辅助) Storyboard