将 UIView 设置在距屏幕右边缘固定比例的位置 iOS
Set a UIView in a fixed proportional position from the Right Edge of screen iOS
说,我有一个 UIImageView
或任何对象,我将它设置在 UIView
作为子视图 CGRectMake
像这样:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(266, 0, 30, 30)];
[imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]];
[headerView addSubview:imgView];
在这里,我将 imgView
的位置设置为 CGRectMake(266, 0, 30, 30)];
,它从左侧 x position
(即 266)计算它的位置。如果我想从屏幕右侧设置 imgView
的位置怎么办?这样,在不同宽度的 iPhone 屏幕中,它显示在相同的比例位置。但是从右边缘算起它的位置。
非常感谢。
嗯,由于ImageView的宽度是30,从左边算起的x坐标是266,所以从右边算起的x坐标是296。
我建议不要对确切位置进行硬编码,而是计算 x 和 y。这样它将有助于不同尺寸的屏幕,因此它不是一个精确的位置,而是基于视图的尺寸。
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
CGFloat padding = 20; // amount for padding
CGSize size = CGSizeMake(30, 30); // size of imageView
CGPoint startPos = CGPointMake(headerView.frame.size.width - padding - size.width, 0); // starting position for imageView
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(startPos.x, startPos.y, size.width, size.height)]; [imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]]; [headerView addSubview:imgView];
说,我有一个 UIImageView
或任何对象,我将它设置在 UIView
作为子视图 CGRectMake
像这样:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(266, 0, 30, 30)];
[imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]];
[headerView addSubview:imgView];
在这里,我将 imgView
的位置设置为 CGRectMake(266, 0, 30, 30)];
,它从左侧 x position
(即 266)计算它的位置。如果我想从屏幕右侧设置 imgView
的位置怎么办?这样,在不同宽度的 iPhone 屏幕中,它显示在相同的比例位置。但是从右边缘算起它的位置。
非常感谢。
嗯,由于ImageView的宽度是30,从左边算起的x坐标是266,所以从右边算起的x坐标是296。
我建议不要对确切位置进行硬编码,而是计算 x 和 y。这样它将有助于不同尺寸的屏幕,因此它不是一个精确的位置,而是基于视图的尺寸。
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
CGFloat padding = 20; // amount for padding
CGSize size = CGSizeMake(30, 30); // size of imageView
CGPoint startPos = CGPointMake(headerView.frame.size.width - padding - size.width, 0); // starting position for imageView
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(startPos.x, startPos.y, size.width, size.height)]; [imgView setImage:[UIImage imageNamed:[self.imageDicKey objectAtIndex:section]]]; [headerView addSubview:imgView];