添加 UINavigationBar 时 Swift 约束错误

标签 swift xcode nslayoutconstraint

我正在尝试向我的应用添加自定义导航栏,但每当它加载时,我都会收到“无法同时满足约束”错误。我似乎无法以编程方式删除任何约束 - 而且我绝对没有添加任何约束。

控制台输出:

"<NSLayoutConstraint:0x600000092750 UILabel:0x7ffc40507d20'Configure'.firstBaseline == UILayoutGuide:0x6000007b18e0'TitleView(0x7ffc40503120)'.top + 23   (active)>",
"<NSLayoutConstraint:0x600000092840 UILabel:0x7ffc40507d20'Configure'.top >= UILayoutGuide:0x6000007b18e0'TitleView(0x7ffc40503120)'.top   (active)>"

应用委托(delegate):

self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController(navigationBarClass: CustomNavBar.self, toolbarClass: nil)
let mainView = TableViewController()
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window!.makeKeyAndVisible()

自定义导航栏类​​:

self.frame.size.height = 60
self.backgroundColor = appColour
self.tintColor = UIColor.white
self.titleTextAttributes = [
            NSForegroundColorAttributeName : UIColor.white,
            NSFontAttributeName : UIFont(name: "Avenir-Heavy", size: 30)!
]
self.setTitleVerticalPositionAdjustment(-5, for: .default)

View Controller :

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(start))
navigationItem.rightBarButtonItem!.imageInsets = UIEdgeInsets(top: -7, left: 0, bottom: 0, right: 0)
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Folder")!, style: .plain, target: self, action: #selector(viewSaved))
navigationItem.leftBarButtonItem!.imageInsets = UIEdgeInsets(top: -7, left: 0, bottom: 0, right: 0)
self.title = "Configure"

我只是不知道该怎么办。

提前致谢!

最佳答案

问题出在您的 CustomNavBar 类中的字体大小,减小字体大小直到您没有收到约束错误。

self.titleTextAttributes = [
        NSForegroundColorAttributeName : UIColor.white,
        NSFontAttributeName : UIFont(name: "Avenir-Heavy", size: <30>)!
]

关于添加 UINavigationBar 时 Swift 约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323618/

相关文章:

swift - 在用户关闭应用程序后请求 HealthKit 权限

ios - 从单独的类输出二维数组中的元素

iphone - iOS数据管理

ios - 多个文本字段的 PickerView 只显示一个数组

ios - 无论 View 高度如何,frame.size.height 的值始终相同

ios - 将 String 转换为 NSDate 但仍然无法访问 dateByAddingTimeInterval 方法

ios - 单击搜索栏时如何导航到另一个 View Controller

ios - 使用 Swift 3.0 编译的模块无法在 Swift 2.3 中导入

ios - 更改固有大小时动画 NSLayoutConstraint 出现问题

ios - Xcode - 向 UITableView 添加约束以使其适合所有屏幕尺寸