【问题标题】:How can I fix constraint in the landscape mode in swift?如何快速修复横向模式下的约束?
【发布时间】:2017-04-27 19:51:14
【问题描述】:

我有一个问题,我该如何解决带视图的横向模式下的约束问题?

我为在视图控制器底部创建的视图设置了约束高度 = 80 但是当设备进入横向模式时,我需要将此视图的高度约束设置为 40 ,但在这种情况下,我对约束存在问题(可能以下列表中的至少一个约束是一个你不想要。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (

   "<NSLayoutConstraint:0x7d175100 UIView:0x7d5eeef0.height == 40   (active)>",
    "<NSLayoutConstraint:0x7d5f8ef0 UIView:0x7d5eeef0.height == 80   (active)>"

)
)

以下是我的代码:

 var toolBarView:UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 80))
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = UIColor(red: 23 / 255.0, green: 120 / 255.0, blue: 104 / 255.0, alpha: 1.0)
        return view
    }()


     func setupToolBarViewInPortrait() {

            toolBarView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            toolBarView.rightAnchor.constraint(equalTo:view.rightAnchor).isActive = true
            toolBarView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            toolBarView.heightAnchor.constraint(equalToConstant: 80).isActive = true

 }

 func setupToolBarViewInLandScape() {

        toolBarView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        toolBarView.rightAnchor.constraint(equalTo:view.rightAnchor).isActive = true
        toolBarView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        toolBarView.heightAnchor.constraint(equalToConstant: 40).isActive = true


    }




override func viewWillLayoutSubviews() {

        let oriantation = UIDevice.current.orientation
        if oriantation.isLandscape {
           setupNavigationBarWithObjectsInLandScape()
           setupToolBarViewInLandScape()

        }else if oriantation.isPortrait {
            setupToolBarViewInPortrait()
        }

    }

【问题讨论】:

    标签: ios swift uiview autolayout constraints


    【解决方案1】:

    由于我在社区中总是迟到,所以发布答案很晚。 我就是这样处理你的案子的。你可以试试。

    一开始我的 View 控制器是这样的。

    对于 iphone 7plus,纵向限制为 128,横向限制为 55。

    我做了什么。

    第 1 步:

    拉出高度限制插座,以便我可以通过编程方式更改它。

    第 2 步:

    我为计算定义了一些预定义的值。

    var  SCREEN_HEIGHT = UIScreen.main.bounds.size.height
    var  BASE_SCREEN_HEIGHT:CGFloat = 736.0 //My base layout is iphone 7 plus and 128(redViewheight) according to this.
    var  HEIGHT_RATIO = SCREEN_HEIGHT/BASE_SCREEN_HEIGHT
    

    第 3 步:

    创建了一个函数并从 viewDidLoad 中调用它作为初始布局常量。

    func  updateConstraint() {
        self.redViewHeight.constant = 128 * HEIGHT_RATIO
    }
    

    第 4 步:

    因为每当旋转变化时都会调用这个方法,所以我调用了这个函数。

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: { context in
            //This function will do the trick
    
            let orientation = UIDevice.current.orientation
            if orientation == .portrait {
                self.redViewHeight.constant = 128 * HEIGHT_RATIO //Just a value
            }
            else {
                self.redViewHeight.constant = 55  * HEIGHT_RATIO //Just a value
            }
            self.view.layoutIfNeeded()
    
        }, completion: {
            _ in
        })
    
    }
    

    希望你会发现这很有用。

    【讨论】:

      【解决方案2】:

      您同时打开了两个高度限制,并且它们发生冲突,因此出现了问题。当您激活横向约束时,您需要使纵向约束不活动,反之亦然。

      解决方案可能是在视图生命周期方法(viewDidLoadviewWillAppear)中一次设置所有约束,将它们存储在类或结构的属性或实例变量中,然后打开它们或方向改变时关闭。

      或者,如果您确定 toolBarView 没有除此代码中更改的约束之外的其他约束,请在调用您的设置方法之前从视图中删除所有约束(不太优雅,并且可能没有那么高性能)。

      【讨论】:

        猜你喜欢
        • 2023-02-21
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-01
        相关资源
        最近更新 更多