我有一个简单的按钮,我想在里面放一个图像,所以我有这个代码:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 13, 116, 138)];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visuel lpath] ofType:@"jpg"]];
[myButton setImage:image forState:UIControlStateNormal];
在InterfaceBuilder中的很容易告诉我的图像采取我的按钮的大小和宽度,但如何做 那个以编程方式?我必须使用myButton.contentHorizontalAlignment = .....这样的东西吗?
最诚挚的问候,
答案 0 :(得分:1)
你可能想要做的就是......
myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
myButton.frame = CGRectMake(20.0, 13.0, 116.0, 138.0);
UIImage *tImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visue1 lpath] ofType:@"jpg"]];
[myButton setBackgroundImage:[tImage stretchableImageWithLeftCapWidth:11 topCapHeight:0] forState:UIControlStateNormal];
[tImage release];
当然,封面宽度取决于您的图像。它只是一遍又一遍地重复下一行像素,直到图像拉伸到适当的宽度