swift - 如何重绘横向的子视图
问题描述
下午好社区,
我正在尝试通过扩展向视图控制器添加两个子视图,但是在旋转设备时它不尊重布局,关于如何使其看起来正确的任何选项?
我在下面附上了我的代码和几张示例照片,第一张照片是我希望它看起来的样子,其余的 ui 都丢失了。
这是我的代码:
func presentCustomAlertController(on ViewController:UIViewController,toPresentCustomView customView:UIView,withBackgroundView backGroundView:UIView){
customView.translatesAutoresizingMaskIntoConstraints = false
guard let targetView = ViewController.view else {return}
backGroundView.alpha = 0
backGroundView.backgroundColor = .black
backGroundView.frame = targetView.bounds
targetView.addSubview(backGroundView)
targetView.addSubview(customView)
customView.setNeedsLayout()
customView.setNeedsDisplay()
customView.layoutIfNeeded()
customView.layoutSubviews()
targetView.setNeedsLayout()
targetView.setNeedsDisplay()
targetView.layoutIfNeeded()
targetView.layoutSubviews()
backGroundView.setNeedsDisplay()
backGroundView.setNeedsLayout()
backGroundView.layoutIfNeeded()
customView.frame = CGRect(x: targetView.center.x, y: targetView.center.y, width: targetView.frame.size.width-80, height: 300)
UIView.animate(withDuration: 0.25, animations: {
backGroundView.alpha = 0.6
},completion: { done in
UIView.animate(withDuration: 0.25, animations: {
customView.center = targetView.center
})
})
}
}
解决方案
推荐阅读
- git - 如何从本地 git 存储库中删除所有敏感信息?
- apache-spark - 我怎么知道镶木地板文件块的大小?
- r - 从逻辑回归结果创建条形图的问题
- javascript - 警告:无法对未安装的组件执行 React 状态更新。这是一个无操作,但它表明您的应用程序中存在内存泄漏
- python - 返回 Pandas Dataframe 中字符串中第一个数字/字母的索引/位置
- javascript - Puppeteer - 如何评估返回文本的 XPath?
- scrapy - Scrapy - 动态文件命名表单解析项
- php - 解析多维数组 PHP
- javascript - 如何在反应中每 5 秒加 1 和 setState
- javascript - 如何在 Javascript 中通过用户操作使反馈消失?