【问题标题】:Clip Image of UIButtonUIButton 的剪辑图像
【发布时间】:2011-07-19 01:33:36
【问题描述】:

我有一个使用图像进行显示的 UIButton。

问题是我的图像是 109x78,但我希望按钮的大小为 109x20,固定在左上角并剪切所有大于 20 像素高的东西。

这就是我现在拥有的:

UIButton *startTrainingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[startTrainingBtn setContentMode:UIViewContentModeTopLeft];
[startTrainingBtn setClipsToBounds:YES];
[startTrainingBtn setImage:[UIImage imageNamed:@"startTrainingClosed"] forState:UIControlStateNormal];
[startTrainingBtn setFrame:CGRectMake(900, 653, 109, 20)];
[self.view addSubview:startTrainingBtn];

我原以为将 ContentMode 设置为 TopLeft 并将 ClipsToBounds 设置为 YES 会起作用,但它只是缩小图像以适应。

【问题讨论】:

    标签: ios cocoa-touch uibutton


    【解决方案1】:

    这是在一年前提出的,我想你已经做了我要告诉你的事情,但我还是把这个留作记录: 然后使用适当大小的图像。您的图像的高度超过了所需高度的两倍。

    我知道这种行为看起来很奇怪,但这是处理按钮图像的方式。 但是,您可以使用图像将背景颜色设置为图案,它会产生您想要的效果。

    UIButton *startTrainingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    startTrainingBtn.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"startTrainingClosed"]];
    [startTrainingBtn setFrame:CGRectMake(900, 653, 109, 20)];
    [self.view addSubview:startTrainingBtn];
    

    此解决方案的问题在于,您无法为不同的状态设置不同的图像,也无法对按钮进行选择效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2017-02-14
      • 2014-11-26
      • 2011-04-10
      • 2015-12-17
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多