2017-03-08 37 views
0

我在視圖控制器中有一個圖像視圖,但很奇怪,我無法在imageview內部顯示圖像。它位於頁面的中間或其他視圖控制器中的其他位置。我的圖像視圖和圖像選擇器的代碼如下。無法在Swift中的圖像視圖區域中看到圖像

image view in the VC

這是輸出。箭頭圖像沒有按」 T顯示在預期範圍:

and this is the output. Arrow image doesn' t show in expected area

profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
profileImageView.bottomAnchor.constraint(equalTo: nameTextField.topAnchor, constant: -12).isActive = true 
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true 
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true 

profileImageView.image = UIImage(named: "arrow_long_left") 
profileImageView.translatesAutoresizingMaskIntoConstraints = false 
profileImageView.contentMode = .scaleAspectFill 
profileImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView))) 
profileImageView.isUserInteractionEnabled = true 
view.addSubview(profileImageView) 



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

     var selectedImageFromPicker: UIImage? 

     if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage { 
      selectedImageFromPicker = editedImage 
     } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage { 
      selectedImageFromPicker = originalImage 
     } 

     if let selectedImage = selectedImageFromPicker { 
      profileImageView.image = selectedImage 
     } 

     dismiss(animated: true, completion: nil) 
    } 

func handleSelectProfileImageView() { 

     let imagePicker = UIImagePickerController() 

     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { 
      imagePicker.delegate = self 
      imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary 
      //picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)! 
      imagePicker.allowsEditing = true 
      self.present(imagePicker, animated: true, completion: nil) 
     } 

     /*else if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) { 
      let picker = UIImagePickerController() 
      picker.delegate = self 
      picker.sourceType = UIImagePickerControllerSourceType.camera; 
      picker.allowsEditing = true 
      present(picker, animated: true, completion: nil) 

     }*/ 
    } 
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
     print("canceled picker") 
     dismiss(animated: true, completion: nil) 
    } 

回答

1

你的代碼是在這裏下車:

profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
profileImageView.bottomAnchor.constraint(equalTo: nameTextField.topAnchor, constant: -12).isActive = true 
profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true 
profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true 

我的猜測是你想要的東西更是這樣的:

profileImageView.leadingAnchor.constraint(equalTo: nameTextField.trailingAnchor, constant:10).isActive = true 
profileImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:10).isActive = true 
profileImageView.topAnchor.constraint(equalTo: nameTextField.topAnchor).isActive = true 
profileImageView.bottomAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true 

如果您想將寬度和高度固定爲150,則需要稍微調整一下。但是,現在你錨定到視圖的中心X,所以它的水平居中。並錨定在nameTextField頂部的底部 - 當然,它位於nameTextField上方

+0

非常感謝dmorrow,它非常棒!但正如你所說當我試圖安排寬度和高度與「profileImageView.widthAnchor.constraint(equalToConstant:80).isActive = true profileImageView.heightAnchor.constraint(equalToConstant:80).isActive = true」代碼塊,它沒有'工作。我該怎麼做,你有什麼建議嗎? – gozdebal

+0

@gozdebal如果您要發送圖像的高度/寬度,則不應設置尾部或底部錨點,因爲這會限制圖像。這完全取決於您希望佈局如何響應不同大小的設備 – dmorrow