Я пытаюсь создать UIView
, у которого есть свойство UIImage
, которое устанавливает 2 UIImageView
. Эффект, который я пытаюсь создать вид профиля и работает в раскадровке:Подкласс UIView с UIImageView внутри не отвечает на автоматическую компоновку
Однако в моем приложении является то, что он не реагирует на авто-макет. В тот момент она конкретизируется в меню вид переполняет меню View Controller:
import UIKit
@IBDesignable
class ProfileImageView: UIView {
@IBInspectable
var profileImage : UIImage = UIImage()
@IBInspectable
var topImageScalePercentage : CGFloat = 50
override init(frame: CGRect) {
super.init(frame: frame)
updateLayout()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
updateLayout()
}
func updateLayout() {
// Create the large background Image View
let blurredProfileView = UIImageView(frame: self.bounds)
blurredProfileView.image = profileImage
blurredProfileView.contentMode = UIViewContentMode.scaleAspectFill
blurredProfileView.layer.masksToBounds = true
// Blur the background Image View
let blurEffect = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = blurredProfileView.bounds
blurredProfileView.addSubview(blurView)
// Add the Blurred Profile View to view
self.addSubview(blurredProfileView)
// Create smaller Profile Picture
var profileRect : CGRect!
if blurredProfileView.bounds.width < blurredProfileView.bounds.height {
let width = blurredProfileView.bounds.width * topImageScalePercentage/100.0
let height = width // 1:1
profileRect = CGRect(x: 0.0, y: 0.0, width: width, height: height)
} else {
let height = blurredProfileView.bounds.height * topImageScalePercentage/100.0
let width = height // 1:1
profileRect = CGRect(x: 0.0, y: 0.0, width: width, height: height)
}
let profileImageView = UIImageView(frame: profileRect)
profileImageView.image = profileImage
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.masksToBounds = true
profileImageView.layer.cornerRadius = profileImageView.bounds.width/2
profileImageView.center = blurredProfileView.center
self.addSubview(profileImageView)
}
}
Примечание: Вышеприведенный код НЕ проверяет, были ли уже добавлены подвидные объекты. Если 'updateLayout' вызывается несколько раз, вы МОЖЕТЕ иметь несколько« blurredProfileView »и другие представления, которые были добавлены в функцию. – Brandon
Я пробовал свой код и в Раскадровке, а также на тестовом устройстве, я получаю это как результат: http://imgur.com/a/Qz9ne. Размытость, а также меньший профиль, похоже, исчезают. Пользовательский UIView полностью ограничен, и ваш код, похоже, добавляет ограничения для профилей, но он не отвечает на него. – Emptyless
Я попытался добавить heightAnchor, так как у моего storyBoard есть якорь высоты (4 ребра и высота): blurredProfileView.heightAnchor.constraint (equalTo: self.heightAnchor), но это не имеет никакого эффекта :( – Emptyless