2017-06-22 49 views
0

我爲我的swift應用程序創建了一個註冊頁面,它包含一個允許用戶選擇個人資料圖片的按鈕,並且該圖片應該替換佔位符「添加圖片」按鈕,一旦用戶通過使用button.setImage()和新選擇的圖片來選擇它。我的問題是,當用戶選擇圖片時,它完全在屏幕上水平延伸,而不是約束到150x150寬度和按鈕的高度。圖片來自UIImagePicker方向不正確

這是我的按鈕:

let plusPhotoButton : UIButton = { 
    let b = UIButton(type: .system) 
    b.imageView?.clipsToBounds = true 
    b.setImage(#imageLiteral(resourceName: "plus_photo").withRenderingMode(.alwaysOriginal), for: .normal) 
    b.addTarget(self, action: #selector(setProfilePic), for: .touchUpInside) 
    return b 
}() 

我把它添加到視圖這樣的:

fileprivate func setupPlusPhotoButton() { 
    view.addSubview(plusPhotoButton) 
    plusPhotoButton.anchor(top: view.topAnchor, paddingTop: 50, left: nil, paddingLeft: 0, right: nil, paddingRight: 0, bottom: nil, paddingBottom: 0, width: 150, height: 150) 
    plusPhotoButton.anchorCenter(x: view.centerXAnchor, y: nil) 
} 

使用此UIView的擴展,使我的錨固容易:

extension UIView { 
func anchor(top: NSLayoutYAxisAnchor?, paddingTop: CGFloat, left: NSLayoutXAxisAnchor?, paddingLeft: CGFloat, right: NSLayoutXAxisAnchor?, paddingRight: CGFloat, bottom: NSLayoutYAxisAnchor?, paddingBottom: CGFloat, width: CGFloat, height: CGFloat) { 

    self.translatesAutoresizingMaskIntoConstraints = false 

    if let top = top { 
     self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true 
    } 
    if let left = left { 
     self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true 
    } 
    if let right = right { 
     self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true 
    } 
    if let bottom = bottom { 
     self.bottomAnchor.constraint(equalTo: bottom, constant: paddingBottom).isActive = true 
    } 
    if width != 0 { 
     self.widthAnchor.constraint(equalToConstant: width) 
    } 
    if height != 0 { 
     self.heightAnchor.constraint(equalToConstant: height).isActive = true 
    } 
} 

func anchorCenter(x: NSLayoutXAxisAnchor?, y: NSLayoutYAxisAnchor?) { 
    self.translatesAutoresizingMaskIntoConstraints = false 
    if let x = x { 
     self.centerXAnchor.constraint(equalTo: x).isActive = true 
    } 
    if let y = y { 
     self.centerYAnchor.constraint(equalTo: y).isActive = true 
    } 
} 

}

我提出我的UIImagePickerController是這樣的:

func setProfilePic() { 
    print("setting profile picture") 
    let imagePickerVC = UIImagePickerController() 
    imagePickerVC.delegate = self 
    imagePickerVC.allowsEditing = true 
    present(imagePickerVC, animated: true, completion: nil) 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage { 
     plusPhotoButton.setImage(editedImage.withRenderingMode(.alwaysOriginal), for: .normal) 
    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage { 
     plusPhotoButton.setImage(originalImage.withRenderingMode(.alwaysOriginal), for: .normal) 
    } 

    plusPhotoButton.layer.cornerRadius = plusPhotoButton.frame.width/2 
    plusPhotoButton.layer.masksToBounds = true 
    plusPhotoButton.layer.borderColor = UIColor.black.cgColor 
    plusPhotoButton.layer.borderWidth = 3 

    dismiss(animated: true, completion: nil) 
} 

任何和所有的幫助表示感謝,謝謝!

回答

0

Nevermind - 只是檢查我的擴展名,並沒有設置我的寬度約束的isActive屬性爲true。 Duh

+0

Duh的確是我的男人 – Chisko