最后更新:2017-06-30

现象描述

新公司做的解决的第一个bug 就是 UIAlterController 不居中,莫名其妙的飞出屏幕之外

找了很久的答案,最终在苹果论坛看到了相关的描述

We have noticed this in an app when we attempt to present a view from the current "top-most" view. If a UIAlertController happens to be the top-most view we get this behavior. We have changed our code to simply ignore UIAlertControllers, but I'm posting this in case others hit the same issue (as I couldn't find anything).

我们注意到,当我们试图从“top-most”的view present 一个view 时候,当 UIAlertController 刚还是 ““top-most”” 的视图,就会出现这个情况

解决方案:

OC:

在 UIAlertControllers分类里面写

  1. /*
  2. * UIAlertControllers (of alert type, and action sheet type on iPhones/iPods) get placed in crazy
  3. * locations when you present a view controller over them. This attempts to restore their original placement.
  4. */
  5. - (void)_my_fixupLayout
  6. {
  7. if (self.preferredStyle == UIAlertControllerStyleAlert && self.view.window)
  8. {
  9. CGRect myRect = self.view.bounds;
  10. CGRect windowRect = [self.view convertRect:myRect toView:nil];
  11. if (!CGRectContainsRect(self.view.window.bounds, windowRect) || CGPointEqualToPoint(windowRect.origin, CGPointZero))
  12. {
  13. CGPoint center = self.view.window.center;
  14. CGPoint myCenter = [self.view.superview convertPoint:center fromView:nil];
  15. self.view.center = myCenter;
  16. }
  17. }
  18. else if (self.preferredStyle == UIAlertControllerStyleActionSheet && self.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPhone && self.view.window)
  19. {
  20. CGRect myRect = self.view.bounds;
  21. CGRect windowRect = [self.view convertRect:myRect toView:nil];
  22. if (!CGRectContainsRect(self.view.window.bounds, windowRect) || CGPointEqualToPoint(windowRect.origin, CGPointZero))
  23. {
  24. UIScreen *screen = self.view.window.screen;
  25. CGFloat borderPadding = ((screen.nativeBounds.size.width / screen.nativeScale) - myRect.size.width) / 2.0f;
  26. CGRect myFrame = self.view.frame;
  27. CGRect superBounds = self.view.superview.bounds;
  28. myFrame.origin.x = CGRectGetMidX(superBounds) - myFrame.size.width / 2;
  29. myFrame.origin.y = superBounds.size.height - myFrame.size.height - borderPadding;
  30. self.view.frame = myFrame;
  31. }
  32. }
  33. }
  34. - (void)viewWillLayoutSubviews
  35. {
  36. [super viewWillLayoutSubviews];
  37. [self _my_fixupLayout];
  38. }

SWIFT

  1. extension UIAlertController {
  2. open override func viewWillLayoutSubviews() {
  3. super.viewWillLayoutSubviews()
  4. fixupLayout()
  5. }
  6. private func fixupLayout() {
  7. if preferredStyle == .alert && view.window != nil {
  8. let myRect = view.bounds
  9. let windowRect = view.convert(myRect, to: nil)
  10. guard let window = view.window else {
  11. return
  12. }
  13. guard let superview = view.superview else {
  14. return
  15. }
  16. if !window.bounds.contains(windowRect) || windowRect.origin.equalTo(CGPoint.zero) {
  17. let myCenter = superview.convert(window.center, from: nil)
  18. view.center = myCenter
  19. }
  20. } else if preferredStyle == .actionSheet && self.traitCollection.userInterfaceIdiom == .phone && view.window != nil {
  21. let myRect = view.bounds
  22. let windowRect = view.convert(myRect, to: nil)
  23. guard let window = view.window else {
  24. return
  25. }
  26. guard let superview = view.superview else {
  27. return
  28. }
  29. if !(window.bounds.contains(windowRect)) || windowRect.origin.equalTo(CGPoint.zero) {
  30. let screen = window.screen
  31. let borderPadding = ((screen.nativeBounds.width / screen.nativeScale) - myRect.size.width) / 2.0
  32. var myFrame = view.frame
  33. let superBounds = superview.bounds
  34. myFrame.origin.x = superBounds.maxX - myFrame.width / 2
  35. myFrame.origin.y = superBounds.height - myFrame.height - borderPadding
  36. self.view.frame = myFrame
  37. }
  38. }
  39. }
  40. }

参考

解决 UIAlterController 不居中问题的更多相关文章

  1. 解决bootstrap模态框居中问题

    完美解决办法: 在bootstrap.js或bootstrap.min.js文件中找到Modal.prototype.show方法. 在that.$element.addClass('in').att ...

  2. 温故而知新 css + html 超级牛逼的居中策略

    该方法甚至可以解决img内容居中的问题 套路:最外层div宽度为居中内容所占的宽度(通常是1170px),并且使其居中(margin:auto) 里层的div宽度为全屏(通常是1920px;)再mar ...

  3. 单独一个img标签的居中显示

    针对页面当中通过img插入图片的时候,要保证这个图片在页面内容当中居中,一般的做法是在外面套一个div,通过给div加入 {margin:0 auto;} 来控制图片的居中. 那么如果针对后台上传的图 ...

  4. Android权限禁止及友好提示用户开通必要权限

    Android权限 Android安全架构规定:默认情况下,任何应用都没有权限执行对其他应用.操作系统或用户有不利影响的任何操作.这包括读写用户的私有数据(联系人,短信,相册,位置).读写其他应用的文 ...

  5. css的小知识3

    1.补充 margin的margin:0 auto:会解决元素的居中,前提是给这个元素设置width 2.css的层叠问题 css有两个性质 1.继承性 2.层叠性  选择器的一种选择能力,谁的权重大 ...

  6. CSS的再深入2(更新中···)

    在上一章中,我们又引出了一个知识点: margin的问题 margin:0 auto:(上下为0,左右自适应)会解决元素的居中问题(auto 自适应)前提是给这个元素设置width 同时,我们又要学习 ...

  7. 从零开始的全栈工程师——html篇1.6

    浮动与伪类选择器 一.浮动(float) 1.标准文档流 标准文档流是一种默认的状态 浏览器的排版是根据元素的特征(块和行级) 从上往下 从左往右排版 这就是标准文档流 2.浮动(float)floa ...

  8. 浏览器CSS兼容

    一.<important 在IE6及FF中的使用>.box1 {width:150px !important;} .box1 {width:250px;} !important是说这个设置 ...

  9. UICollectionView 图片横向滑动居中偏移量的解决

    1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分. 2.使用方法前不要开启 ...

随机推荐

  1. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...

  2. django后台集成富文本编辑器Tinymce的使用

    富文本编辑器Tinymce是使用步骤: 1.首先去python的模块包的网站下载一个django-tinymce的包 2.下载上图的安装包,然后解压,进入文件夹,执行: (pychrm直接运行命令pi ...

  3. neo4j源码分析1-编译打包启动

    date: 2018-03-22 title: "neo4j源码分析1-编译打包启动" author: "邓子明" tags: - 源码 - neo4j - 大 ...

  4. 关于ES6的新特性

    1  let声明变量 01    let声明的变量,不可重复声明,比如 let   a=1  : let   a=2 :这样申明会报错 02    let声明的变量,有块级作用域,比如  if( ){ ...

  5. Hadoop本地模式搭建

    官方文档,不同版本修改url地址中的数字即可 http://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-common/Single ...

  6. android——屏幕适配

    一,基本概念 1:dip: 其实也就是dp,与像素无关 2:px: 像素,在安卓布局中不用px,因为每个手机像素不同,px显示的布局大小也就不同 3:dpi: 通俗点就是每英寸多少个像素,简称像素密度 ...

  7. netserver启动时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC'"

    netperf启动netserver时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family A ...

  8. python打包命令

    打包成exe方法 (1)切换到该文件夹 (2)pyinstaller -F py文件 (py文件要英文才行) -F 生成单个可执行文件 -w 去掉控制台窗口 -p 自定义需要加载的类路径 -i 可执行 ...

  9. tp5 模板参数配置(模板静态文件路径)

    tp5 模板参数配置(模板静态文件路径) // 模板页面使用 <link rel="stylesheet" type="text/css" href=&q ...

  10. ID学习一 Basic

    Assignment 作用:定义变量并赋值 变量可以是新定义的也可以是已经存在的: 值可以是另一个变量的值.一个文本值.一个复杂的表达式(利用表达式编辑助手构造): 注意:一旦变量被定义,你不能删除变 ...