UIButton角半径与背景图片

时间:2019-11-28 13:55:00

标签: ios swift iphone xcode uibutton

我正在尝试建立一个自定义的UIButton,它经过四舍五入并且内部有图像。 我可以得到圆形的按钮,但是在配置了按钮的拐角半径后,图像消失了。

我发现了this堆栈溢出问题,但是尝试了此处提到的解决方案后,该按钮仍未显示图像。

下面添加了屏幕截图:

任何帮助将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:4)

var folder = "Views/assets/images"; debugger; $.ajax({ url: folder, success: function (data) { var patt1 = /"([^"]*\.(jpe?g|png|gif))"/gi; // extract "*.jpeg" or "*.jpg" or "*.png" or "*.gif" var result = data.match(patt1); result = result.map(function (el) { return el.replace(/"/g, ""); }); // remove double quotes (") surrounding filename+extension // TODO: do this at regex! var uniqueNames = []; // this array will help to remove duplicate images $.each(result, function (i, el) { var el_url_encoded = encodeURIComponent(el); // avoid images with same name but converted to URL encoded // console.log("under analysis: " + el); if ($.inArray(el, uniqueNames) === -1 && $.inArray(el_url_encoded, uniqueNames) === -1) { // console.log("adding " + el_url_encoded); uniqueNames.push(el_url_encoded); $("#slider").append("<img src='" + el_url_encoded + "' alt=''>"); // finaly add to HTML } // else{ console.log(el_url_encoded + " already in!"); } }); $(".grid li").each((i, li) => { let bg = folder + uniqueNames[Math.floor(Math.random() * uniqueNames.length) + 1]; $(li).css('background-image', 'url(' + bg + ')'); setInterval(() => { let bg = folder + uniqueNames[Math.floor(Math.random() * uniqueNames.length) + 1]; $(li).css('background-image', 'url(' + bg + ')'); }, 20000); }) console.log(uniqueNames) }, error: function (xhr, textStatus, err) { alert('Error: here we go...'); alert(textStatus); alert(err); alert("readyState: " + xhr.readyState + "\n xhrStatus: " + xhr.status); alert("responseText: " + xhr.responseText); } }); 内致电super.layoutSubviews()

或仅删除layoutSubviews()函数即可完成操作:)