0

I am working on an iPad UI and a button on that UI needs to have this image:

http://imgur.com/tVkP8wd

(as a PNG). The button is being declared like this:

CGRect newNoteButtonRect = CGRectMake(0, 0, 69, 43);                         
UIButton* newNoteButton = [[UIButton alloc] initWithFrame:newNoteButtonRect];
newNoteButton.imageView.contentMode = UIViewContentModeScaleToFill;
[newNoteButton setImage:self.fNewNoteIcon forState:UIControlStateNormal];

where 'fNewNoteIcon' is a UIImage. When the UI comes up, the image is tiny and squished, and almost nothing I do can change that. Any ideas?

The icon is initialized like this:

self.fNewNoteIcon = [UIImage imageNamed:@"New_Note.png"];
4

2 回答 2

0

在提供的代码中,您没有指定UIButton类型?使用自定义图像创建按钮

// Create image
self.fNewNoteIcon = [UIImage imageNamed:@"buttomImage.png"];

// Create rect for button. 0, 0 and size it from image
CGRect newNoteButtonRect = CGRectMake:(0, 0, _fNewNoteIcon.size.width, _fNewNoteIcon.size.height);

// Alloc and init UIButton with type
// Pass in image. Add to subview of view.
UIButton *newNoteButton = [UIButton buttonWithType: UIButtonTypeCustom];
[newNoteButton setImage: _fNewNoteIcon forState: UIControlStateNormal];
[self.view addSubview: newNoteButton];

我在没有编译的情况下在 SO 中输入了所有这些内容,因此请检查一下,但这应该会为您提供一个 UIButton,并且所需的图像尺寸正确。

于 2013-08-29T22:46:18.123 回答
0

所以看起来如果你的 PNG 没有 Alpha 通道,那么你就会遇到大小问题。我在 Photoshop 中打开了 PNG 并添加了一个 Alpha 通道。图像现在在 iPad 中的大小正确!

于 2013-08-29T23:03:31.287 回答