了解纵横比约束和乘数

Understanding the Aspect ratio constraint and multiplier

我以编程方式创建了一个 UIScrollview。我在这个滚动视图上添加了一些以编程方式创建的按钮。按钮高度为 30。但按钮宽度会根据屏幕大小而变化。

float scrollYearX=btnLeftArrow.frame.origin.x+btnLeftArrow.frame.size.width+15;
float scrollYearY=40.0;
float scrollYearWidth=btnRightArrow.frame.origin.x-15-scrollYearX;
float scrollYearHeight=50.0;
scrollYear=[[UIScrollView alloc] initWithFrame:CGRectMake(scrollYearX, scrollYearY, scrollYearWidth, scrollYearHeight)];

[scrollYear setShowsHorizontalScrollIndicator:NO];
[scrollYear setShowsVerticalScrollIndicator:NO];

[vwParent addSubview:scrollYear];
int buttonX=0;

for (int i=0; i<100; i++) {
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, scrollYearWidth/5, 30)];
    [btn.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];

    int mod=i%2;
    if (mod==1) {
        [btn setTitle:@"-" forState:UIControlStateNormal];
        [btn.titleLabel setTextColor:[UIColor whiteColor]];

    }
    else
    {
        [btn addTarget:self action:@selector(yearClick :) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:[NSString stringWithFormat:@"%li",(long)backYear] forState:UIControlStateNormal];
        if (backYear==year) {
            btnCurrentYear=btn;
            [btn setBackgroundImage:[UIImage imageNamed:@"yearSelect"] forState:UIControlStateNormal];
        }
        backYear++;
    }
    [scrollYear addSubview:btn];
    btnLast=btn;
    buttonX=buttonX+btn.frame.size.width;
}
[scrollYear setContentSize:CGSizeMake(btnLast.frame.origin.x+btnLast.frame.size.width, scrollYearHeight)];
[scrollYear setContentOffset:CGPointMake(btnCurrentYear.frame.origin.x-(btnLast.frame.size.width*2),0)];

我的问题是我应该使用哪个约束才能在每个设备上正确显示它。在这里,我附上了它在 6s 和 ipad pro 上的显示方式。我怎样才能使 ipad pro 视觉效果与 6s 相同。

按钮在 6 秒内打开

按钮在 ipad pro 中看起来

请帮助我。 谢谢

这是因为您的按钮背景宽度已缩放。您需要使用可调整大小的图像来保持圆边。使用以下内容更改您的背景图片:

UIImage* backgroundImage = [UIImage imageNamed:@"yearSelect"];
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backgroundImage.size.width/2-1, 0, backgroundImage.size.width/2)];
[btn setBackgroundImage:backgroundImage forState:UIControlStateNormal];