[英]Align button at centre of Button iOS
I have a button whose size may vary as per requirement. 我有一个按钮,其大小可能会根据要求而有所不同。 But I need the image that is set on it, to always remain at its centre.
但是我需要树立的形象始终保持其中心。
The code I'm using right now is: 我现在使用的代码是:
[pAddButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[pAddButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[pAddButton setContentMode:UIViewContentModeCenter];
However, when I increase the width of the button, the images stretches automatically. 但是,当我增加按钮的宽度时,图像会自动拉伸。 That is what I'm trying to prevent.
这就是我要防止的事情。
将此代码图像框尝试到按钮
[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];
您是否尝试过将imageView的内容模式设置为居中,例如:
pAddButton.imageView.contentMode = UIViewContentModeCenter;
Set the image using setImage:forState:
instead of setBackgroundImage:forState:
. 使用
setImage:forState:
而不是setBackgroundImage:forState:
设置图像。
If that doesn't help you could use imageEdgeInsets
to reduce the image's width. 如果那没有帮助,则可以使用
imageEdgeInsets
减小图像的宽度。
您可以使用setBackgroundImage的setImage instesd或在按钮[button addSubView:imageView]上将图像添加为subView。
您是否尝试通过以下方式简单地将图像居中:
myImageView.center = pAddButton.center;
Try this, 尝试这个,
CGPoint centerImageView = imageView.center;
centerImageView = pAddButton.center;
ImageView.center = centerImageView;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.