ฉันเพิ่งเริ่มแอปสำหรับ iOS ใน Swift ฉันกำลังออกแบบโดยทางโปรแกรม (ฉันหมายความว่าฉันไม่ได้ใช้สตอรี่บอร์ด)
ฉันมี UIButton ที่มีรูปภาพอยู่ข้างใน ฉันต้องการให้รูปภาพเต็มปุ่มทั้งหมด ตอนนี้มันแค่แสดงภาพในปุ่มของฉัน แต่เล็กกว่าปุ่ม โดยอยู่ตรงกลางด้านบนของกรอบปุ่ม
รหัสของฉันคือ:
let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize))
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.myExtension()
นี่คือสิ่งที่ฉันได้ลอง (วิทยานิพนธ์ไม่ได้ผล):
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.autoresizingMask = .flexibleWidth
self.autoresizingMask = .flexibleHeight
self.imageView?.contentMode = .scaleAspectFill
ถ้าฉันใส่บรรทัด contentMode มันก็ไม่ทำงานเช่นกัน imageView จะไม่ส่งคืนหากฉันได้ตรวจสอบแล้ว ..
ขอบคุณ!
buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)
- person Rocky   schedule 24.05.2019