如何相对于ios中的图像动态更改UI按钮的宽度和高度

时间:2015-03-31 09:08:23

标签: ios objective-c iphone

如何相对于ios中的图像动态更改UIButton的宽度和高度?

[[btn_dynamic imageView] setContentMode: UIViewContentModeScaleToFill];
//    btn_dynamic.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
//    btn_dynamic.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//[btn_dynamic setContentMode:UIViewContentModeScaleAspectFit];

我在这些区域尝试但没有工作

7 个答案:

答案 0 :(得分:2)

//Get the size of image

 UIImage *buttonImage = [UIImage imageNamed:@"btnImage"];

 CGSize buttonSize = CGSizeMake(buttonImage.size.width, buttonImage.size.height);

 buttonInstance = [UIButton buttonWithType:UIButtonTypeCustom];

 [buttonInstance setImage:buttonImage forState:UIControlStateNormal];

//Set the frame of button using image size

 [buttonInstance setFrame:CGRectMake(0, 0, buttonSize.width, buttonSize.height)];

答案 1 :(得分:0)

您需要创建自定义按钮,如下所示

 UIButton *btnDynamic = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnDynamic setImage:[UIImage imageNamed:@"image_Name"] forState:UIControlStateNormal];

它只是根据图像创建自定义按钮..

希望它有所帮助......!

答案 2 :(得分:0)

如果您不想触摸按钮框并想要设置看起来不那么有弹性的图像。请使用下面的代码

btn_dynamic.imageView.contentMode = UIViewContentModeScaleAspectFit;

如果要设置按钮宽度和高度,则需要根据图像大小设置按钮大小。

UIImage *image = [UIImage imageNamed:@"image.jpg"];
CGRect btnFrame = btn_dynamic.frame;
btnFrame.size.width = image.size.width;
btnFrame.size.height = image.size.height;
[btn_dynamic setFrame:btnFrame];

如果您想保持比率,因为图像可能比视图大

UIImage *image = [UIImage imageNamed:@"image.jpg"];
CGRect btnFrame = btn_dynamic.frame;
btnFrame.size.width = 100; // lets the button width is 100 or set according to the need.
btnFrame.size.height = (btnFrame.size.width*image.size.height)/image.size.width;
[btn_dynamic setFrame:btnFrame];

答案 3 :(得分:0)

您可以使用以下内容更改按钮大小。

CGSize imageSize = image.size;
CGRect rectButton = button.frame;

rectButton.size = imageSize
button.frame = rectButton

答案 4 :(得分:0)

试试此代码

    float x=0;

    for(int i = 100; i < 110; i++)
     {
         btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

         UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];

        [btn setBackgroundImage:img forState:UIControlStateSelected];

        titleString = [titleArray objectAtIndex:i-100];  // get button title

       CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];

        CGRect currentFrame = btn.frame;

        CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);

       [btn setFrame:buttonFrame];

        x = x + fontSize.width + 35.0;

        [btn setTitle:titleString forState: UIControlStateNormal];

        [btn setTag:i];

        [self.view addSubview:btn];
   }

答案 5 :(得分:0)

试试这个简单的解决方案

TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);

[TestButton sizeToFit];

float width  =   TestButton.frame.size.width + 20;

TestButton.frame = CGRectMake(TestButton.frame.origin.x, 
TestButton.frame.origin.y, width, 40);

答案 6 :(得分:0)

谁在寻找一个自动布局解决方案,我所做的是我保持温度,文本颜色清晰的颜色,在uibutton的底部,并将自动布局设置为, 1. uibutton left = tmp uilabel left 2. uibutton right = tmp uilabel right

因此,通过设置tmp标签文本,标签将根据文本自动调整其宽度,因为标签和按钮左右相等,按钮也会自动扩展其宽度。

希望这会有所帮助..

相关问题