簡體   English   中英

帶有圖像和文本的UIButton不起作用

[英]UIButton with image and text doesn't work

我搜索了解決方案,我在這里看到幾個類似的帖子,但我無法找到答案。 我想添加一個設置圖像的UIButton(背景顏色也很好)我想在這個圖像上添加文字

到目前為止這是我的代碼:

        labelSorry.text = @"לא נמצאו תוצאות.";//add text
        //add button.
        Request =[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *requestimg = [UIImage imageNamed:@"requestcontact_resize2.png"];
        Request.frame = CGRectMake(0.0, 120, 320.0, 30.0);
        Request.titleLabel.textAlignment = NSTextAlignmentRight;
       // Request.titleLabel.textColor = UIColorFromRGB(0xffffff);
        [Request setBackgroundImage:requestimg forState:UIControlStateNormal];
        Request.imageView.image = [UIImage imageNamed:@"requestcontact_resize2.png"];//see no image :( this is kidding me

      //  [Request setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 120, 320.0, 30.0)];///You can replace it with your own dimensions.
        label.textColor = UIColorFromRGB(0xffffff);
        label.text = @"בקש פרטי איש קשר";
        [Request addSubview:label];

        //[Request setBackgroundColor:UIColorFromRGB(0x704c9f)];
        Request.titleLabel.text = @"בקש פרטי איש קשר";
        [Request addTarget:self action:@selector(requestDetails) forControlEvents:UIControlEventTouchUpInside];
        [noResultsView addSubview:Request];
        [self.view addSubview:noResultsView];

我可以看到按鈕的背景,但我看不到任何文字:(也許有人可以啟發我?

你可以看到我嘗試了各種解決方案(我把它們放在評論中),但都沒有。

PS我沒有使用xib。

任何幫助將受到高度贊賞。

您應該使用[button setImage:forState:][button setTitle:forState:] 如果要為按鈕設置背景圖像,則應使用[button setBackgroundImage:forState:] 此外,如果需要,您可以更改圖像和標題的位置,以便使用[button setImageEdgeInsets:][btn setTitleEdgeInstes:]

暫無
暫無

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

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