重新加载视图控制器并以编程方式更改 UIImageView 约束
Reload View Controller and programatically change UIImageView constraint
我有一个 UIImageView,它的高度受用户拍摄或从他们的库中选择的照片的纵横比影响。在用户选择照片后,我无法让 UIImageView 适当(或根本)调整大小。
代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//let mediaType2 : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let mediaType : CFString = info[UIImagePickerControllerMediaType] as! CFString
if mediaType == kUTTypeMovie {
let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path!) {
UISaveVideoAtPathToSavedPhotosAlbum(path!, self, "video:didFinishSavingWithError:contextInfo:", nil)
}
}
else{
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
let multiplyNum = screenSize.width / img.size.width
imageView.frame = CGRectMake(0, 0, screenSize.width, multiplyNum*img.size.height)
imageView.image = img
}
self.dismissViewControllerAnimated(true, completion: {})
}
目前在情节提要中设置了约束,但我想以编程方式覆盖 imageView 的约束。
您应该更新 UIImageView 的约束而不是设置框架。为图像视图设置一个高度约束,然后为该约束创建一个 IBOutlet(控制从约束拖动到它的视图控制器),将其命名为 "imageViewHeightConstraint." 然后在您的代码中说:
imageViewHeightConstraint.constant = multiplyNum*img.size.height
@beyowulf 的答案有效且易于实现
但是如果不想设置constraint outlet,可以在代码中指定ratio属性,也很简单:
//remove the last image ration constraint
if let ratioConstrain = self.ratioConstrain {
self.imageView.removeConstraint(ratioConstraint)
}
self.ratioConstraint = NSLayoutConstraint(
item: self.imageView, attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: self.imageView,
attribute: NSLayoutAttribute.Height,
multiplier: image!.size.width / image!.size.height,
constant: 0)
self.imageView.addConstraint(self.ratioConstraint!)
不要使用无效的框架
在这种情况下,您需要移除 imageView 的高度约束,并将宽度设置为与超级视图相等,您将得到您想要的:屏幕宽度相等,高度随图像本身的比例变化。
有一个问题,如果你从你的 roll 中得到的图像太小,你可能希望宽度小于屏幕,在这种情况下你应该将 imageView 宽度约束设置为 lessThanOrEqual(<=),并添加一些位置约束(例如垂直居中,水平居中),这样看起来会更好。
我有一个 UIImageView,它的高度受用户拍摄或从他们的库中选择的照片的纵横比影响。在用户选择照片后,我无法让 UIImageView 适当(或根本)调整大小。
代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//let mediaType2 : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let mediaType : CFString = info[UIImagePickerControllerMediaType] as! CFString
if mediaType == kUTTypeMovie {
let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path!) {
UISaveVideoAtPathToSavedPhotosAlbum(path!, self, "video:didFinishSavingWithError:contextInfo:", nil)
}
}
else{
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
let multiplyNum = screenSize.width / img.size.width
imageView.frame = CGRectMake(0, 0, screenSize.width, multiplyNum*img.size.height)
imageView.image = img
}
self.dismissViewControllerAnimated(true, completion: {})
}
目前在情节提要中设置了约束,但我想以编程方式覆盖 imageView 的约束。
您应该更新 UIImageView 的约束而不是设置框架。为图像视图设置一个高度约束,然后为该约束创建一个 IBOutlet(控制从约束拖动到它的视图控制器),将其命名为 "imageViewHeightConstraint." 然后在您的代码中说:
imageViewHeightConstraint.constant = multiplyNum*img.size.height
@beyowulf 的答案有效且易于实现
但是如果不想设置constraint outlet,可以在代码中指定ratio属性,也很简单:
//remove the last image ration constraint
if let ratioConstrain = self.ratioConstrain {
self.imageView.removeConstraint(ratioConstraint)
}
self.ratioConstraint = NSLayoutConstraint(
item: self.imageView, attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: self.imageView,
attribute: NSLayoutAttribute.Height,
multiplier: image!.size.width / image!.size.height,
constant: 0)
self.imageView.addConstraint(self.ratioConstraint!)
不要使用无效的框架 在这种情况下,您需要移除 imageView 的高度约束,并将宽度设置为与超级视图相等,您将得到您想要的:屏幕宽度相等,高度随图像本身的比例变化。
有一个问题,如果你从你的 roll 中得到的图像太小,你可能希望宽度小于屏幕,在这种情况下你应该将 imageView 宽度约束设置为 lessThanOrEqual(<=),并添加一些位置约束(例如垂直居中,水平居中),这样看起来会更好。