โหมดเนื้อหา UIButton ImageView ไม่ทำงาน

ฉันเพิ่งเริ่มแอปสำหรับ 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 จะไม่ส่งคืนหากฉันได้ตรวจสอบแล้ว ..

ขอบคุณ!


person Quentin Rth    schedule 24.05.2019    source แหล่งที่มา
comment
ลอง buttonLocation.contentMode = .scaleAspectFit   -  person Ben Rockey    schedule 24.05.2019
comment
ใช้ buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)   -  person Rocky    schedule 24.05.2019
comment
@BenRockey มันใช้งานไม่ได้   -  person Quentin Rth    schedule 24.05.2019
comment
ลองตั้งค่าโหมดเนื้อหาของวัตถุ imageView ภายในปุ่ม อดีต. buttonLocation.imageView?.contentMode = .scaleAspectFill   -  person BhargavR    schedule 24.05.2019
comment
@BhargavR ไม่ทำงานเช่นกัน :( และฉันได้ลอง setBackgroundImage แล้วและฉันก็มีปัญหาเดียวกันทุกประการ   -  person Quentin Rth    schedule 24.05.2019


คำตอบ (4)


ตั้งค่า contentMode ของ buttonLocation's imageView เป็น scaleAspectFill หรือ scaleToFill เพื่อครอบคลุม frame ของ button ทั้งหมด

let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 500))
buttonLocation.setImage(UIImage(named: "image"), for: .normal)
buttonLocation.backgroundColor = .gray
buttonLocation.layer.cornerRadius = buttonLocation.frame.width / 2
buttonLocation.imageView?.contentMode = .scaleAspectFill //Here........

ตัวอย่าง:

ป้อนคำอธิบายรูปภาพที่นี่

person PGDev    schedule 24.05.2019
comment
นั่นคือสิ่งที่ฉันทำอย่างที่คุณเห็นในโพสต์แรกของฉัน และมันก็ไม่ได้ผล - person Quentin Rth; 24.05.2019

คุณต้องใช้คุณสมบัติ setBackgroundImage แทน setImage

buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)
person Samir Shaikh    schedule 24.05.2019
comment
ปัญหาเดียวกันกับ setBackgroundImage แทน setImage - person Quentin Rth; 24.05.2019

บางทีนี่อาจช่วยคุณได้:

let buttonLocation = UIButton(type: .custom)
buttonLocation.frame = CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize)
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.imageView?.contentMode = .scaleAspectFill
buttonLocation.contentHorizontalAlignment = .fill
buttonLocation.contentVerticalAlignment = .fill
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.clipsToBounds = true

คุณต้องเพิ่มคุณสมบัติ contentHorizontalAlignment และ contentVerticalAlignment และอย่าลืมตั้งค่า clipsToBounds ให้เป็นจริงเพื่อดูเอฟเฟกต์มุม

person riza milani    schedule 24.05.2019
comment
ปัญหาเดียวกันแม้จะมีทั้งหมดนั้น - person Quentin Rth; 24.05.2019
comment
คุณเรียกอะไรใน myExtension() ลองลบ imageEdgeInsets และทดสอบอีกครั้ง - person riza milani; 24.05.2019

ลองใช้โค้ดด้านล่างและลบคุณสมบัติอื่น ๆ ที่ตั้งค่าไว้สำหรับปุ่มนี้:

let buttonLocation = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
        buttonLocation.setImage(UIImage(named: "cat"), for: .normal)
        buttonLocation.backgroundColor = .white
        buttonLocation.layer.cornerRadius = 50
        buttonLocation.imageView?.contentMode = .scaleAspectFill
        buttonLocation.clipsToBounds = true

        self.view.addSubview(buttonLocation)

ตรวจสอบให้แน่ใจว่าภาพที่คุณกำลังตั้งค่าในปุ่มมีขนาดใหญ่กว่ากรอบของปุ่ม คุณสามารถเปลี่ยนค่ารัศมีของเฟรมและมุมได้ในโค้ดด้านบน ฉันเพิ่งลองด้วยโค้ดด้านบนและได้รับผลลัพธ์ที่ต่ำกว่า

ป้อนคำอธิบายรูปภาพที่นี่

คุณสามารถแสดงความคิดเห็นได้หากนี่ไม่ใช่ผลลัพธ์ที่คุณคาดหวัง

หวังว่านี่จะช่วยได้

person BhargavR    schedule 24.05.2019
comment
มันยังคงใช้งานไม่ได้และขนาดภาพของฉันก็สูงกว่าขนาดปลดกระดุม ดังนั้นฉันจึงไม่เข้าใจว่าทำไมมันถึงใช้งานไม่ได้... - person Quentin Rth; 27.05.2019