我的VC中有一个UIScrollview包含9个图像视图,大小为25px x 25像素,在iPhone 5s上它们在滚动视图的中心位置完美。当在iphone 6上运行时,由于屏幕尺寸的原因,它们被推到左侧,而不是在屏幕上均匀拉伸。
有没有办法可以让它们在两种屏幕尺寸上均匀分布?
以下代码:
// Set up ScrollView NSLog(@"%f", self.view.frame.size.width); UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(25, 200, self.view.frame.size.width - 50, 30)]; scrollView.alwaysBounceHorizontal = YES; NSLog(@"%f", scrollView.frame.size.width); UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; [scrollView addSubview:view]; UIImage *star = [UIImage imageNamed:@"badge_star"]; UIImageView *imgView = [[UIImageView alloc] initWithImage:star]; imgView.frame = CGRectMake(2.5, 5, 25, 25); [view addSubview:imgView]; UIImage *target= [UIImage imageNamed:@"badge_target"]; UIImageView *targetImageView = [[UIImageView alloc] initWithImage:target]; targetImageView.frame = CGRectMake(32.5, 5, 25, 25); [view addSubview:targetImageView]; UIImage *crown = [UIImage imageNamed:@"badge_crown"]; UIImageView *crownImageView = [[UIImageView alloc] initWithImage:crown]; crownImageView.frame = CGRectMake(62.5, 5, 25, 25); [view addSubview:crownImageView]; UIImage *scissors = [UIImage imageNamed:@"badge_scissors"]; UIImageView *scissorsImageView = [[UIImageView alloc] initWithImage:scissors]; scissorsImageView.frame = CGRectMake(92.5, 5, 25, 25); [view addSubview:scissorsImageView]; UIImage *thumbsup = [UIImage imageNamed:@"badge_thumbs_up"]; UIImageView *thumbsupImageView = [[UIImageView alloc] initWithImage:thumbsup]; thumbsupImageView.frame = CGRectMake(122.5, 5, 25, 25); [view addSubview:thumbsupImageView]; UIImage *trophy = [UIImage imageNamed:@"badge_trophy"]; UIImageView *trophyImageView = [[UIImageView alloc] initWithImage:trophy]; trophyImageView.frame = CGRectMake(152.5, 5, 25, 25); [view addSubview:trophyImageView]; UIImage *medal = [UIImage imageNamed:@"badge_medal"]; UIImageView *medalImageView = [[UIImageView alloc] initWithImage:medal]; medalImageView.frame = CGRectMake(182.5, 5, 25, 25); [view addSubview:medalImageView]; UIImage *heart = [UIImage imageNamed:@"badge_heart"]; UIImageView *heartImageView = [[UIImageView alloc] initWithImage:heart]; heartImageView.frame = CGRectMake(212.5, 5, 25, 25); [view addSubview:heartImageView]; UIImage *coathanger = [UIImage imageNamed:@"badge_coathanger"]; UIImageView *coathangerImageView = [[UIImageView alloc] initWithImage:coathanger]; coathangerImageView.frame = CGRectMake(242.5, 5, 25, 25); [view addSubview:coathangerImageView]; [self.view addSubview:scrollView]; }
答案 0 :(得分:0)
那是因为你很难编写滚动视图的起点。你有两个选择;
1.增加每个图像之间的间距或
2.更改滚动视图的起点
在您的代码中选择第二个选项相对容易。将以下代码用于滚动视图框。
float scrollViewWidth = 25*9+5*8+2*2.5; // Cross check this one.
float xScrollPoint = (self.view.frame.size.width - scrollViewWidth)/2
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(xScrollPoint, 200, scrollViewWidth, 30)];