首页 文章

UIView是半透明导航栏的后面,具有实用的视图约束

提问于
浏览
0

我有一个函数,将通用 UIView 插入到视图顶部的视图控制器中,我希望插入的视图位于导航栏的底部,或者如果没有,则在底部状态栏 .

我认为视图控制器顶部布局指南会在嵌入导航控制器时自动更新,但似乎并非如此 .

这是我到目前为止所拥有的:

init(in viewController: UIViewController) {
    super.init(frame: CGRect(x: 0, y: 0,
                             width: viewController.view.frame.size.width, height: 50))
    initializeSubviews()

    viewController.view.addSubview(self)

    self.topAnchor.constraint(equalTo: viewController.topLayoutGuide.bottomAnchor, constant: 0).isActive = true
    self.leadingAnchor.constraint(equalTo: viewController.view.leadingAnchor, constant: 0).isActive = true
    viewController.view.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
    self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50))

    self.setNeedsLayout()
}

我可以使用占位符视图在界面构建器中实现此目的,因此我尝试使用与此处使用的约束锚相同的约束锚:

Xcode interface builder showing view constraints

问题是视图始终位于Y位置0并且位于导航栏后面 . 我无法更改导航栏的半透明设置 .

加载视图时,我在控制台中收到以下错误:

<MyApp.MyReusableView: 0x101f3b590; frame = (0 0; 0 0); layer = <CALayer: 0x17003e720>>
2017-01-09 17:07:21.347965 MyApp[2483:984880] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x1742862c0 h=--& v=--& MyApp.MyReusableView:0x101f3b590.width == 0   (active)>",
    "<NSLayoutConstraint:0x1702882f0 H:|-(0)-[MyApp.MyReusableView:0x101f3b590]   (active, names: '|':UIView:0x101f29f00 )>",
    "<NSLayoutConstraint:0x170288340 H:[MyApp.MyReusableView:0x101f3b590]-(0)-|   (active, names: '|':UIView:0x101f29f00 )>",
    "<NSLayoutConstraint:0x174286450 'UIView-Encapsulated-Layout-Width' UIView:0x101f29f00.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170288340 H:[MyApp.MyReusableView:0x101f3b590]-(0)-|   (active, names: '|':UIView:0x101f29f00 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-01-09 17:07:21.350923 MyApp[2483:984880] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x174280a50 h=-&- v=-&- UIView:0x102b38f70.width == MyApp.MyReusableView:0x101f3b590.width   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x1742862c0 h=--& v=--& MyApp.MyReusableView:0x101f3b590.width == 0   (active)>",
    "<NSLayoutConstraint:0x174286090 H:|-(16)-[UILabel:0x102b23d20'\Uf170']   (active, names: '|':UIControl:0x102b22d10 )>",
    "<NSLayoutConstraint:0x174285ff0 H:[UILabel:0x102b23d20'\Uf170']-(8)-[UILabel:0x102b2efa0'Test Notification']   (active)>",
    "<NSLayoutConstraint:0x174285f50 H:[UILabel:0x102b2efa0'Test Notification']-(0)-|   (active, names: '|':UIControl:0x102b22d10 )>",
    "<NSLayoutConstraint:0x174285cd0 H:|-(0)-[UIControl:0x102b22d10]   (active, names: '|':UIView:0x102b38f70 )>",
    "<NSLayoutConstraint:0x174285870 H:[UIButton:0x102b0d040'\Uf05e']-(0)-|   (active, names: '|':UIView:0x102b38f70 )>",
    "<NSLayoutConstraint:0x1742857d0 H:[UIControl:0x102b22d10]-(0)-[UIButton:0x102b0d040'\Uf05e']   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174285ff0 H:[UILabel:0x102b23d20'']-(8)-[UILabel:0x102b2efa0'Test Notification']   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-01-09 17:07:21.352456 MyApp[2483:984880] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x174286360 h=--& v=--& MyApp.MyReusableView:0x101f3b590.height == 0   (active)>",
    "<NSLayoutConstraint:0x170288390 MyApp.MyReusableView:0x101f3b590.height == 50   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170288390 MyApp.MyReusableView:0x101f3b590.height == 50   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-01-09 17:07:21.354622 MyApp[2483:984880] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<_UILayoutSupportConstraint:0x1702877b0 _UILayoutGuide:0x101f3ae30.height == 64   (active)>",
    "<_UILayoutSupportConstraint:0x17009d420 V:|-(0)-[_UILayoutGuide:0x101f3ae30]   (active, names: '|':UIView:0x101f29f00 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x174286270 h=--& v=--& MyApp.MyReusableView:0x101f3b590.midY == 0   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x174286360 h=--& v=--& MyApp.MyReusableView:0x101f3b590.height == 0   (active)>",
    "<NSLayoutConstraint:0x170287f30 V:[_UILayoutGuide:0x101f3ae30]-(0)-[MyApp.MyReusableView:0x101f3b590]   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x174286540 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' UIView:0x101f29f00.minY == 0   (active, names: '|':UIViewControllerWrapperView:0x102b09100 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170287f30 V:[_UILayoutGuide:0x101f3ae30]-(0)-[MyApp.MyReusableView:0x101f3b590]   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-01-09 17:07:21.356571 MyApp[2483:984880] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x174280a50 h=-&- v=-&- UIView:0x102b38f70.width == MyApp.MyReusableView:0x101f3b590.width   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x1742862c0 h=--& v=--& MyApp.MyReusableView:0x101f3b590.width == 0   (active)>",
    "<NSLayoutConstraint:0x174285f00 UIButton:0x102b0d040'\Uf05e'.width == 50   (active)>",
    "<NSLayoutConstraint:0x174285cd0 H:|-(0)-[UIControl:0x102b22d10]   (active, names: '|':UIView:0x102b38f70 )>",
    "<NSLayoutConstraint:0x174285870 H:[UIButton:0x102b0d040'\Uf05e']-(0)-|   (active, names: '|':UIView:0x102b38f70 )>",
    "<NSLayoutConstraint:0x1742857d0 H:[UIControl:0x102b22d10]-(0)-[UIButton:0x102b0d040'\Uf05e']   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174285f00 UIButton:0x102b0d040''.width == 50   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我对这个视图约束做错了什么?

1 回答

  • 1

    确保为视图和viewController.view设置了translatesAutoresizingMaskIntoConstraints为false

相关问题