簡體   English   中英

UIButton標題上方的中心圖像

[英]Center image above UIButton title

我有自定義的UIButton,視圖的高度/寬度為1/4。 我想做的是在按鈕標題上方設置一個圖像,使它們兩個都位於該按鈕的中心。

碼:

CGRect imageFrame= CGRectMake(0, 0, 80, 80);
UIEdgeInsets imageInset = UIEdgeInsetsMake(0, 0, 30, 0);
UIEdgeInsets titleInset = UIEdgeInsetsMake(80, 0, 0, 0);

btnSideSettings = [UIButton buttonWithType:UIButtonTypeCustom];
btnSideSettings.frame = settingsFrame;
[btnSideSettings setImage:[UIImage imageNamed:@"btnSideSettings"] forState:UIControlStateNormal];
btnSideSettings.imageView.frame = imageFrame;
btnSideSettings.imageEdgeInsets = imageInset;
btnSideSettings.titleEdgeInsets = titleInset;
btnSideSettings.titleLabel.textAlignment = NSTextAlignmentCenter;
[btnSideSettings setTitle:@"Settings" forState:UIControlStateNormal];

結果: 在此處輸入圖片說明

我嘗試了上述代碼的一些變體,主要是編輯插圖。 如果我注釋掉添加圖片,標題也將居中。 感謝任何幫助。

可以嘗試添加自己的子視圖(圖像視圖和標簽),然后對元素的大小和位置進行完全而明確的控制,而不必嘗試在按鈕上操作圖像和文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM