ios 如何在iphone中以编程方式实现带有背景图像的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5012305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:54:18  来源:igfitidea点击:

how to implement button with backgroundimage programmatically in iphone

iphoneiosuibutton

提问by user549767

I'm developing images in view with button programmatically in iphone. now I want to add button backgroundimage for next and previous buttons. I want to add a images for these buttons programmatically.

我正在以编程方式在 iphone 中使用按钮开发图像。现在我想为下一个和上一个按钮添加按钮背景图像。我想以编程方式为这些按钮添加图像。

how to add button image in iphone.

如何在iphone中添加按钮图像。

回答by Sudhanshu

// Add the button like this

// 像这样添加按钮

UIImage *redButtonImage = [UIImage imageNamed:@"ExitButton.png"];

        UIButton *redButton = [UIButton buttonWithType:UIButtonTypeCustom];
        redButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
        [redButton setBackgroundImage:redButtonImage forState:UIControlStateNormal];

        [view addSubview:redButton];

回答by Kshitiz Ghimire

you can try this

你可以试试这个

 UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];


//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

[button addTarget:self action:@selector(clickActionItem:) forControlEvents:UIControlEventTouchUpInside];

good luck

祝你好运

回答by Ishu

use this for setting image in you button

使用它在你的按钮中设置图像

[yourButton setBackgroundImage:yourImage forState:UIControlStateNormal];

回答by Yash Joshi

Try this,

尝试这个,

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(0, 0, 80, 40);
[yourButton setBackgroundImage:[UIImage imageNamed:@"backgroungImage.png"] forState:UIControlStateNormal];
[self.view yourButton];