iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用snapkit设置autolayout后,iOS框架出错大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_197_1@
我在UIStoryboard中添加一个UIView并将其绑定到一个名为testView的自定义UIView类,接下来,我在requireView函数的textView中创建了一个名为subView的UIView,

这是我的一步

1初始化subView

2将新的子视图添加到textView

3设置自动布局

4 set cornerRadius(view.frame.height / 2)

运行应用程序后,cornerRadius不会更改

然后我尝试打印subView的框架,得到(0,0)

这是我的代码

required init?(coder aDecoder: NSCoder) {
   super.init(coder: aDecoder)

   circelView = UIView()
   self.addSubview(circelView)
   circelView.snp_makeConsTraints(closure: { (makE) -> Void in
       make.size.equalTo(80)
       make.top.equalTo(self.snp_top)
       make.right.equalTo(self.snp_right)
   })
   print(circelView.framE) //get wrong frame
   circelView.layer.cornerRadius = circelView.frame.size.height / 2
   circelView.layer.masksToBounds = true
}

解决方法

在打印框架和设置角半径时,您的视图没有时间布局.添加AutoLayout约束不会自动布局视图.

要获得正确的结果,您需要在布局视图后设置角半径.这将保证您的框架受AutoLayout约束的约束.

为此,请在“viewDidLayoutSubviews”中放置任何需要正确框架的代码

override func viewDidLayoutSubviews() {
    print(circelView.framE) // The frame will have been set
    circelView.layer.cornerRadius = circelView.frame.size.height / 2
}

viewDidLayoutSubviews()是UIViewController上可以覆盖的方法,请查看文档here.

@H_673_44@

大佬总结

以上是大佬教程为你收集整理的使用snapkit设置autolayout后,iOS框架出错全部内容,希望文章能够帮你解决使用snapkit设置autolayout后,iOS框架出错所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。