簡體   English   中英

設置內容插圖將UIButton移到底部時,為什么UIButton將標題標簽一直向右移?

[英]Why does the UIButton move the title label all the way to the right when I set content insets to move it to the bottom?

我試圖用文本上方的圖像制作一個UIButton。 為此,我嘗試將按鈕的邊緣插入設置為:

[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 10, 0.0f)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(10, 0.0, 0.0, 0.0)];

我得到的是一個按鈕,在該按鈕中圖像位於同一位置,文本被壓縮到一個側面,大約一個字符寬。

如何簡單地設置插圖,使圖像位於文字上方? 這真的有很多要問的嗎?

您只是錯誤地計算了edgeInSets。 這是我的測試代碼,確實花費了我一段時間才能將圖像和標題放在正確的位置。

UIButton *tmp_testBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 60, 40)];
tmp_testBtn.backgroundColor = [UIColor grayColor];
[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
[tmp_testBtn setImage:[UIImage imageNamed:@"France"] forState:UIControlStateNormal];
[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, -258, 0, 0)];
[tmp_testBtn setTitle:@"testbutton" forState:UIControlStateNormal];

CGRect contentRect = [tmp_testBtn contentRectForBounds:tmp_testBtn.bounds];
NSLog(@"contenRect:%f,%f,%f,%f",contentRect.origin.x,contentRect.origin.y,contentRect.size.width,contentRect.size.height);
CGRect titleRect = [tmp_testBtn titleRectForContentRect:contentRect];
NSLog(@"titleRect:%f,%f,%f,%f",titleRect.origin.x,titleRect.origin.y,titleRect.size.width,titleRect.size.height);
CGRect imageRect = [tmp_testBtn imageRectForContentRect:contentRect];
NSLog(@"imageRect:%f,%f,%f,%f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.size.height);

[self.view addSubview:tmp_testBtn];
[tmp_testBtn release];

順便說一句,我不會這樣,我更喜歡自定義帶有UIImageView和UILabel的按鈕。

暫無
暫無

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

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