[英]How to show the title under the UIButton?
我使用標題插圖在UIButton
下顯示文本名稱。
這是我的代碼和輸出:
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
這是我的預期輸出:
我使用了UIButton
並將標題和UIImage
設置為背景。 那么如何在UIButton
下顯示標題標簽呢?
你試圖做的是正確的; 你只需要按照下面的方法設置插入邊緣。 然后你需要增加你的按鈕框架,以便它可以同時容納標題和圖像。
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];
這也可以通過故事板來完成,因為@VD Patel 建議這取決於您喜歡哪種方法。
您還可以使用單獨的UIImageview
和一個更易於處理且效率更高的按鈕來執行此操作。
請先在xib中選擇按鈕。 然后選擇屬性檢查器,在“Edge”中的這個 Select Title 中,並根據要求設置適當的“Inset”。
請查看以下代碼並根據您的要求設置插圖。
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[myButton setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal];
[myButton setTitle:@"Rate us on app store" forState:UIControlStateNormal];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects myButton
[myButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[myButton setBackgroundColor:[UIColor greenColor]];
[self.view myButton];
創建UIImageView
、 UILabel
和UIButton
。 然后將 ImageView 和 Label 作為子視圖添加到 Button。
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 200, 100)];
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setText:@"Mono"];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.image = [UIImage imageNamed:@"your_image.png"];
[imgView setUserInteractionEnabled:NO];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:imgView];
[btn addSubview:lbl];
[btn setFrame:CGRectMake(0, 20, 200, 300)];
[btn addTarget:self action:@selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
嘗試在 viewDidLoad 方法中添加此代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.