繁体   English   中英

在Button iOS中心对齐按钮

[英]Align button at centre of Button iOS

我有一个按钮,其大小可能会根据要求而有所不同。 但是我需要树立的形象始终保持其中心。

我现在使用的代码是:

[pAddButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [pAddButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [pAddButton setContentMode:UIViewContentModeCenter];

但是,当我增加按钮的宽度时,图像会自动拉伸。 这就是我要防止的事情。

将此代码图像框尝试到按钮

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

使用setImage:forState:而不是setBackgroundImage:forState:设置图像。

如果那没有帮助,则可以使用imageEdgeInsets减小图像的宽度。

您可以使用setBackgroundImage的setImage instesd或在按钮[button addSubView:imageView]上将图像添加为subView。

您是否尝试通过以下方式简单地将图像居中:

myImageView.center = pAddButton.center;

尝试这个,

CGPoint centerImageView = imageView.center;

centerImageView = pAddButton.center;

ImageView.center = centerImageView;

暂无
暂无

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

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