如何在ios中动态改变UI按钮的宽度和高度

问题描述:

如何在ios中动态改变UIButton的宽度和高度?如何在ios中动态改变UI按钮的宽度和高度

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

我在这些地区,但没有工作

+0

你想如果你的按钮的位置改变了,你的按钮的位置改变了吗? – CarmeloS 2015-03-31 09:55:51

试试这个代码

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]; 
    } 

试图您需要创建自定义按钮,如下

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

它简单地创建一个自定义按钮,每个图像..

希望它有助于...!

+1

此代码不会自动获取按钮帧。它将需要0宽度和0高度。请仅在完全确定的情况下发布代码,否则请在发布前进行测试。 – 2015-03-31 09:42:09

//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)]; 
+0

非常感谢您的回复... – 2015-03-31 12:21:01

+0

我想加载服务按钮列表并添加到视图中。所有的按钮都有不同的高度和宽度加载不同的坐标可以请你帮我.... – 2015-04-08 06:39:39

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

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]; 

较大,您可以使用以下方法来改变按钮的大小。

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

rectButton.size = imageSize 
button.frame = rectButton 

试试这个简单的解决方案

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); 

谁曾找一个自动布局解决方案我所做的是我一直在用文字颜色鲜明的色彩的临时uilable ,在uibutton的底部,并设置自动布局为: 1. uibutton left = tmp uilabel left 2. uibutton right = tmp uilabel right

所以通过设置tmp标签文本,标签会根据文本自动调整其宽度,并且由于标签和按钮的左右方向相同,按钮也会自动扩展其宽度。

希望这会有所帮助..