【问题标题】:AutoLayout incorrectly rendering subview's positionAutoLayout 错误地渲染子视图的位置
【发布时间】:2016-01-21 11:51:03
【问题描述】:

我正在学习自动布局,我使用snapkit。我写了一些代码,但工作方式与预期不同。我编写代码leftMargin,但它的工作方式就像它是一个rightMargin。你可以在照片上看到。我的代码有什么问题?

我的代码

let container = View()
 container.backgroundColor=UIColor.greenColor()

 let v1 = View()
 v1.backgroundColor=UIColor.blackColor()
self.view.addSubview(container);
        container.addSubview(v1)

let padding2 : UIEdgeInsets = UIEdgeInsetsMake(20,20,20,20)


        container.snp_makeConstraints { (make) -> Void in


            make.top.equalTo(self.view).offset(padding2.top)

            make.bottom.equalTo(self.view).offset(-padding2.bottom)

            // make.left.equalTo(self.view).inset(padding2.left)

            make.left.equalTo(self.view).offset(padding2.left)

            make.right.equalTo(self.view).offset(-padding2.right)

            //make.width.equalTo(self.view.bounds.width-90)

          /*
            make.top.equalTo(self.view).offset(20)
            make.left.equalTo(self.view).offset(20)
            make.bottom.equalTo(self.view).offset(-20)
            make.right.equalTo(self.view).offset(-20)
             */
        }


        let padding : UIEdgeInsets = UIEdgeInsetsMake(50, 50, 15, 10)




        v1.snp_makeConstraints { (make) -> Void in


         make.topMargin.equalTo(container).offset(padding.top);
         make.leftMargin.equalTo(container).offset(padding.left);

            make.width.equalTo(100);
            make.height.equalTo(100);
        }

【问题讨论】:

    标签: ios iphone swift autolayout ios-autolayout


    【解决方案1】:

    替换

    make.topMargin.equalTo(container).offset(padding.top);
    make.leftMargin.equalTo(container).offset(padding.left);
    

    make.top.equalTo(container).offset(padding.top);
    make.left.equalTo(container).offset(padding.left);
    

    leftleftMargin的区别?检查这个:https://stackoverflow.com/a/28692783/3331750

    【讨论】:

    • 是的,工作得很好。谢谢。但是@justlike snapkit 好不好?如果我专注于它,我会有问题或好的图书馆?
    • @ErhanDemirci snapkit 和 Masonry(OC) 一样酷。
    【解决方案2】:

    我从未使用过 snapkit,但在常规自动布局中,您可以通过以下方式实现您想要的。

        let container = UIView()
        container.backgroundColor=UIColor.greenColor()
    
        let v1 = UIView()
        v1.backgroundColor=UIColor.blackColor()
    
        self.view.addSubview(container)
        container.addSubview(v1)
    
        // enable the views to be resized by auto layout
        container.translatesAutoresizingMaskIntoConstraints = false
        v1.translatesAutoresizingMaskIntoConstraints = false
    
        // pin the container to all edges of the main view
        container.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true
        container.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor).active = true
        container.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
        container.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
    
        // anchor the v1 subview to the
        v1.topAnchor.constraintEqualToAnchor(container.topAnchor, constant: 20).active = true
        v1.leadingAnchor.constraintEqualToAnchor(container.leadingAnchor, constant: 20).active = true
    
       // set a fixed height and width
        v1.heightAnchor.constraintEqualToConstant(100).active = true
        v1.widthAnchor.constraintEqualToConstant(100).active = true
    

    对于比例高度和宽度,将最后两行代码替换为:

        v1.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 0.25).active = true
        v1.heightAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier: 0.25).active = true
    

    【讨论】:

    • 我是 ios 新手。我正在学习自动布局。有人说苹果自动布局不好。你认为呢 ?好还是不好 ?我很困惑。
    • @ErhanDemirci for iOS 9 及更高版本的锚使 AutoLayout 更易于理解和实现,尤其是在以编程方式执行此操作时。顺便说一句,这些blogposts 可能会有所帮助。我是在试图了解这一切是如何运作的时候写的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多