如何在 iPhone 中通过编程实现带有背景图片的按钮

22

我正在以编程方式在iPhone上开发带有按钮的图像视图。现在我想为下一个和上一个按钮添加背景图片。我想以编程方式为这些按钮添加图像。

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

4个回答

41

// 将按钮添加如下

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

8

你可以尝试这个。

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

祝你好运


4

使用此代码为按钮设置图像

[yourButton setBackgroundImage:yourImage forState:UIControlStateNormal];

0

试试这个,

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接