本文介绍了在 swift 中从 UIScrollView 上的 xib 添加 UIView 后如何更新约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器中有一个 UIViewScroll(背景颜色为蓝色).我需要一个来自 Xib 的 UIView(背景颜色是白色).Xib 视图有一个带有约束的 UILabel(背景颜色为绿色).现在,问题是 UILabel 约束在添加到 scrollView 后没有应用.如何在不丢失约束的情况下添加 UIView?请参阅以下屏幕截图和代码.

注意:

我只需要更新 UIView 的子视图的约束,而不使用 NSLayoutConstraintsIBOutlets.

UIView 在 Xib 上:

代码:

class ViewController: UIViewController {@IBOutlet var scrollView:UIScrollView!var profileView:UIView!覆盖 func viewDidLoad() {super.viewDidLoad()self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as!界面视图self.scrollView.addSubview(self.profileView)}覆盖 func viewDidLayoutSubviews() {super.viewDidLayoutSubviews()self.profileView.layer.frame.size = CGSize(width: self.scrollView.frame.width, height: self.profileView.frame.height)self.profileView.layer.position = CGPoint(x: self.scrollView.frame.width/2, y: (self.profileView.frame.height/2)+10)}}

输出:

更新:更多信息

我知道要设置 scrollView 的 contentSize.我使用 UIViewlayer 属性来操作 UIView 的高度和宽度.除了改变高度和宽度之外,我还需要更新 UIView 的子视图的约束.

这是一个理解的例子.但是,实际上我会添加更多这样的视图.

Github 存储库:

解决方案

最好的办法是以编程方式设置 self.profileView 的约束,我在下面添加了一个示例来帮助您入门.

class TestVC: UIViewController {@IBOutlet var scrollView:UIScrollView!私有变量 profileView:UIView!覆盖 func viewDidLoad() {super.viewDidLoad()self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as!界面视图self.configureProfileView()}私有函数 configureProfileView() ->空白 {self.profileView.translatesAutoresizingMaskIntoConstraints = falseself.scrollView.addSubview(self.profileView)self.profileView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor).isActive = trueself.profileView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true//将个人资料视图固定到滚动视图的顶部self.profileView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true}}

更多信息也可以在这里找到:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

I have a UIViewScroll(background color is blue) in view controller. I need a UIView(background color is white) that were from Xib. The Xib view has a UILabel(background color is green) with constraints. Now, the problem is UILabel constraints not applied after adding it to scrollView. How to add UIView without loss of constraints? Refer following screenshots and code.

Note:

I need to just update constraints of the sub views of the UIView without using IBOutlets of NSLayoutConstraints.

UIView on Xib:

Code:

class ViewController: UIViewController {

    @IBOutlet var scrollView: UIScrollView!

    var profileView:UIView!

    override func viewDidLoad() {
        super.viewDidLoad()


        self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
        self.scrollView.addSubview(self.profileView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.profileView.layer.frame.size = CGSize(width: self.scrollView.frame.width, height: self.profileView.frame.height)
        self.profileView.layer.position = CGPoint(x: self.scrollView.frame.width/2, y: (self.profileView.frame.height/2)+10)
    }

}

Output:

Update: More Information

I am aware to set contentSize of the scrollView. I used layer properties of UIView for manipulating height and width of the UIView. Instead of changing height and width also I need to update constraints of the sub views of UIView.

This is an example for understanding. But, In real I will be add more views like that.

Github Repository :https://github.com/RAJAMOHAN-S/ScrollViewTest

Required output:

解决方案

Your best bet is to set the constraint of self.profileView programmatically, I've added an example below to get you started.

class TestVC: UIViewController {

    @IBOutlet var scrollView: UIScrollView!

    private var profileView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
        self.configureProfileView()
    }

    private func configureProfileView() -> Void {

        self.profileView.translatesAutoresizingMaskIntoConstraints = false

        self.scrollView.addSubview(self.profileView)
        self.profileView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor).isActive = true
        self.profileView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true

        // Pin the profile view to the top of the scrollView
        self.profileView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
    }
}

More information can be found here too: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

这篇关于在 swift 中从 UIScrollView 上的 xib 添加 UIView 后如何更新约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:06