不管设备如何,以编程方式从屏幕底部对齐标签-iOS [英] Programmatically align a label from bottom of screen regardless of device - iOS

查看:81
本文介绍了不管设备如何,以编程方式从屏幕底部对齐标签-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过

self.lblTimer = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,height)]; 

..但是由于我要定位的标签位于屏幕的底部,因此在我切换设备时看不到它.我正在尝试使标签从左起10像素,从底部起10像素.无论在iPhone 4、5、6或6 plus上查看它,它都应该看起来一致.

..but since the label i'm looking to position is at the bottom of the screen it goes out of view when i switch devices. I'm trying to make the label 10 pixels in from the left and 10 pixels up from the bottom. It should look consistent regardless if it's viewed on iPhone 4, 5, 6 or 6 plus.

推荐答案

所以,您的问题是关于X和Y的,对不对? iOS中的点从屏幕的左上角开始,因此屏幕的左上角是(0,0).

So, your question is about X and Y, right? The points in iOS start at the top left corner of the screen, so the top left corner of the screen is (0,0).

如果沿左边缘向上/向下移动,则X坐标将继续为0.因此,如果您想在左边缘右边获得10个点,则X将为10.

If you move up/down along the left edge, the X coordinate will continue to be 0. So if you want 10 points to the right of the left edge, X will be 10.

可以通过以下方式计算屏幕底部:

The bottom of the screen can be calculated this way:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

在这里,变量screenHeight将是屏幕的底行.因此,如果您想在线上获得10点,则需要获得(screenHeight-10).

In here, the variable screenHeight will be the bottom line of the screen. So if you want 10 points above the line, you'll need to get (screenHeight - 10).

但是,正如Lyndsey Scott在下面的评论中提到的那样,这会将标签的左上角放在顶部屏幕的高度-10,这可能(很可能值为10)使标签不可见. 为了解决这个问题,您还可以减去标签的高度,以便在正确的位置看到标签.

However, as mentioned by Lyndsey Scott in the comments below, this places the top left corner of your label in the top screenHeight - 10, which could (very likely with a value of 10) place your label out of sight. To solve this, you also subtract the height of the label so the label is in sight, in the correct spot.

所以您的最终答案是:

CGRectMake(10,(screenHeight - height - 10),width,height);

这篇关于不管设备如何,以编程方式从屏幕底部对齐标签-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆