簡體   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