且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不管设备如何,以编程方式从屏幕底部对齐标签-iOS

更新时间:2023-12-04 16:44:04

所以,您的问题是关于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);