如何调整渐变背景的按钮大小?

时间:2016-09-01 06:40:36

标签: ios objective-c

我在尝试调整按钮背景时遇到一些问题,我在界面构建器中使用自动调整大小和5.5英寸iPhone屏幕。在iPhone 6plus和6s plus按钮看起来很像界面构建器,但在6和5s我看到按钮背景宽度在4和4.7英寸屏幕等于按钮背景界面生成器中。但是,当我按下按钮时,它会重绘并且看起来正确,因为我想要(第二个截图)。这是截图。我该如何解决?

first screenshot enter image description here

这是我的代码,它位于viewDidLoad

//** Gradient for red buttons **\\

    colorsForRedButtonGradient = [NSArray arrayWithObjects:
                                  (id)[UIColor colorWithRed:139.0/255.0 green:2.0/255.0 blue:2.0/255.0 alpha:1.0].CGColor,
                                  (id)[UIColor colorWithRed:155.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0].CGColor,
                                  nil];
    highlightedColorsForRedButtonGradient = [NSArray arrayWithObjects:
                                             (id)[UIColor colorWithRed:139.0/255.0 green:2.0/255.0 blue:2.0/255.0 alpha:0.5].CGColor,
                                             (id)[UIColor colorWithRed:155.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0].CGColor,
                                             nil];

    NSArray *locationForRedButtonGradient = [NSArray arrayWithObjects:
                                             [NSNumber numberWithFloat:0.0f],
                                             [NSNumber numberWithFloat:1.0f],
                                             nil];

    gradientLayerForSoundButton = [CAGradientLayer layer];
    gradientLayerForPopupButton = [CAGradientLayer layer];
    gradientLayerForVibroButton = [CAGradientLayer layer];

    gradientLayerForVibroButton.frame = _vibroButton.layer.bounds;
    gradientLayerForSoundButton.frame = _soundButton.layer.bounds;
    gradientLayerForPopupButton.frame = _popupButton.layer.bounds;

    gradientLayerForVibroButton.colors = colorsForRedButtonGradient;
    gradientLayerForVibroButton.locations = locationForRedButtonGradient;
    gradientLayerForSoundButton.colors = colorsForRedButtonGradient;
    gradientLayerForSoundButton.locations = locationForRedButtonGradient;
    gradientLayerForPopupButton.colors = colorsForRedButtonGradient;
    gradientLayerForPopupButton.locations = locationForRedButtonGradient;

    gradientLayerForVibroButton.cornerRadius = _vibroButton.layer.cornerRadius;
    [_vibroButton.layer insertSublayer:gradientLayerForVibroButton atIndex:0];
    gradientLayerForSoundButton.cornerRadius = _soundButton.layer.cornerRadius;
    [_soundButton.layer insertSublayer:gradientLayerForSoundButton atIndex:0];
    gradientLayerForPopupButton.cornerRadius = _popupButton.layer.cornerRadius;
    [_popupButton.layer insertSublayer:gradientLayerForPopupButton atIndex:0];

1 个答案:

答案 0 :(得分:0)

看起来,你已经为所有按钮提供了静态宽度。您需要将所有按钮放在视图中并设置其前导和放大器。尾随约束。

您还可以将min-max width设置为所有按钮以接管不同设备的宽度控制,或者您还可以指定要在viewcontroller中查看的最小和最大宽度。

因此,这可以解决您的问题。

此致

-vikas。