简体   繁体   English

如何调整UIButton的大小以适合UIImageView?

[英]How to resize UIButton to fit UIImageView?

I have a UIButton over a UIImageView. 我在UIImageView上有一个UIButton。 I want the button to adjust its size to fit the imageview. 我希望按钮调整其大小以适合imageview。 I tried a few different ways but can't get it. 我尝试了几种不同的方法,但无法实现。 Anyone know how to do this? 有人知道怎么做吗?

You can add some padding like this: 您可以像这样添加一些填充:

NSInteger padding = 10;
CGRect newFrame = myImageView.bounds;
newFrame.origin.x += padding / 2.0;
newFrame.origin.y += padding / 2.0;
newFrame.size.width -= padding;
newFrame.size.height -= padding;
myButton.frame = newFrame;

尝试这个

    [btn sizeThatFits:CGSizeMake(img.size.width, img.size.height)];

只需在Interface-Builder中创建一个带有图像视图的自定义按钮即可。

you can use-- 您可以使用 -

UIImage *img = [UIImage imageNamed:@"7.png"];// image to be use
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];// set button frame accordingly.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM