xcode 如何找到iOS设备屏幕的动态中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13089149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find dynamic centre of iOS device screen
提问by Vinod Vishwanath
Possible Duplicate:
iOS SDK: Moving the button into the center of screen by code
UIImageView. Zoom and Center to arbitrary rectangle. Can't determine correct center on screen
可能的重复:
iOS SDK:通过代码UIImageView将按钮移动到屏幕中心
。缩放并居中到任意矩形。无法确定正确的屏幕中心
I'm trying to centralise an UIActivityIndicator in a UIWebView(which in turn is a subView of a UIScrollView, which also has a UIToolbar element to the left - although the left-toolbar isn't always visible)
我正在尝试将 UIActivityIndicator 集中在 UIWebView 中(它又是 UIScrollView 的子视图,它的左侧也有一个 UIToolbar 元素 - 尽管左侧工具栏并不总是可见)
Similar questions have been asked before, but the point is in finding the center "dynamically" i.e. on change of orientation as well as presence or absence of the left toolbar.
之前已经问过类似的问题,但重点是“动态”找到中心,即方向的变化以及左侧工具栏的存在或不存在。
What's the best approach? Is there any better way to do this than overriding the method shouldAutorotateToInterfaceOrientation
?
最好的方法是什么?有没有比覆盖该方法更好的方法shouldAutorotateToInterfaceOrientation
呢?
回答by mayuur
Center of what btw?
顺便说一句,中心是什么?
You can access the UIViewController's center as self.view.center
您可以访问 UIViewController 的中心作为 self.view.center
or in your case, UIWebView's center as yourwebView.center
或者在您的情况下,UIWebView 的中心为 yourwebView.center
And by giving it resizing elements of top left, activityindicator would center it-selves always.
通过给它调整左上角的元素,activityindicator 将始终将其自身居中。
EDIT :
编辑 :
If you want center of the screen that would be gained by frame like
如果您想要通过帧获得的屏幕中心,例如
Consider activity indicator of width and height 30, 30.
考虑宽度和高度 30、30 的活动指示器。
(([UIScreen mainScreen].bounds.size.width)/2 - 15, ([UIScreen mainScreen].bounds.size.height)/2 - 15, 30, 30);
Then, set autoresizing elements to none.
然后,将 autoresizing 元素设置为 none。
Go to size Inspector and remove all the arrows from the Auto-sizing feature.
转到大小检查器并从自动调整大小功能中删除所有箭头。
回答by Andrei Filip
There can be not general solution to your question. This is because only you know what element in your view you want to ignore or not. The view always stays the same, no matter what element you add to it. So, I suggest getting a rectangle for the part of the view you consider as clear/empty/available and setting your loading indicator in that. Just get the view's whole frame (self.view.frame.size.height) and substract any elements from there. For example
您的问题没有通用的解决方案。这是因为只有您知道您想忽略或不想忽略视图中的哪个元素。无论您添加什么元素,视图始终保持不变。因此,我建议为您认为清晰/空/可用的视图部分获取一个矩形,并在其中设置加载指示器。只需获取视图的整个框架(self.view.frame.size.height)并从那里减去任何元素。例如
MyIndicator *indicator = [[MyIndicator alloc] initWithFrame:CGRectMake(self.toolbar.frame.size.width + self.toolbar.frame.origin.x, self.topBar.frame.size.height + self.topBar.frame.origin.y, self.view.frame.size.width - self.toolbar.frame.size.width - self.toolbar.frame.origin.x, self.view.frame.size.height - self.topBar.frame.size.height - self.topBar.frame.origin.y)];