UIViewController 내부에 하위 뷰를 추가했습니다. 이 하위 뷰 (PlayerView)에는 두 가지 상태가 있어야합니다. 하나는 높이가 56 인 축소 상태입니다. 펼쳐진 상태의 높이는 385입니다. 하위보기가 축소에서 확장으로 변경되면 PlayerView 내의 하위보기에서 모양과 위치를 변경해야합니다. PlayerView가있는 UIViewController (MasterViewController)에 추가 될 때버튼을 눌렀을 때 앵커/제약 조건 변경
, 그것과 같이() layoutSubviews를 사용하여 축소 된보기를 누르면
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = UIColor(red: 24/255, green: 24/255, blue: 24/255, alpha: 1)
roundCorners([.topLeft, .topRight], radius: 10)
addSubview(albumImage)
addSubview(songTitleLabel)
addSubview(songArtistLabel)
addSubview(playPauseButton)
_ = albumImage.anchor(nil, left: leftAnchor, bottom: nil, right: nil, topConstant: 0, leftConstant: 20, bottomConstant: 0, rightConstant: 0, widthConstant: 40, heightConstant: 40)
albumImage.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
_ = playPauseButton.anchor(nil, left: nil, bottom: nil, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 30, widthConstant: 22, heightConstant: 22)
_ = songTitleLabel.anchor(albumImage.topAnchor, left: albumImage.rightAnchor, bottom: nil, right: playPauseButton.leftAnchor, topConstant: 0, leftConstant: 8, bottomConstant: 0, rightConstant: 8, widthConstant: songTitleLabel.frame.width, heightConstant: songTitleLabel.frame.height)
_ = songArtistLabel.anchor(songTitleLabel.bottomAnchor, left: songTitleLabel.leftAnchor, bottom: nil, right: playPauseButton.leftAnchor, topConstant: 2, leftConstant: 8, bottomConstant: 0, rightConstant: 8, widthConstant: songArtistLabel.frame.width, heightConstant: songArtistLabel.frame.height)
playPauseButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
, 확장 할 예정이다.
let pv = PlayerView()
func expandPlayerView() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
self.pv.frame = CGRect(x: 0, y: CGFloat((self.view.frame.maxY) - 352), width: 375, height: 350)
self.pv.changePlayerView()
self.view.layoutSubviews()
}) { (true) in
}
}
changePlayerView()
기능은 다음과 같이 정의된다 :
func changePlayerView() {
subviews.forEach({$0.removeFromSuperview()})
addSubview(playPauseButton)
playPauseButton.removeConstraints(playPauseButton.constraints)
_ = playPauseButton.anchor(nil, left: nil, bottom: bottomAnchor, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 22, heightConstant: 22)
playPauseButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
는 : 여기 (이 내 MasterViewController 내부에) 그 일에 대해 가서 어떻게 1. playerView는 385로 정밀도가 확장됩니다. 2. 다른 모든 하위 뷰는 플레이어에서 제거됩니다.3. 재생 일시 중지 단추는 거의 전체 PlayerView
하위 뷰를 차지하며 너비와 높이 제한이 22
playPauseButton이 모든 제약 조건을 준수하도록하려면 어떻게해야합니까?
편집 :
.anchor
기능과 같이있는 UIView의 확장에 정의되어
func anchor(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {
translatesAutoresizingMaskIntoConstraints = false
var anchors = [NSLayoutConstraint]()
if let top = top{
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant))
}
if let left = left{
anchors.append(leftAnchor.constraint(equalTo: left, constant: leftConstant))
}
if let bottom = bottom{
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant))
}
if let right = right{
anchors.append(rightAnchor.constraint(equalTo: right, constant: -rightConstant))
}
if widthConstant > 0{
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant))
}
if heightConstant > 0{
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant))
}
anchors.forEach({$0.isActive = true})
return anchors
}
난 당신의 코드를 살펴보기가 힘듭니다. 아마 코딩 스타일 차이 일 것입니다. 그러나 ** ** ** 정의되거나 명명 된 제약 조건이 없습니다. * 기존 제약 조건 배열을 활성화/비활성화하지 않습니다. 그것들은 정의 된 ** constraint **를 변경하는 두 가지 일반적인 방법입니다. 내가 놓친 게 있니? 바꾸어 말하면, 변경하고자하는 * 단일 * 제약 조건 (예 : 높이 또는 너비)을 변경하는 방법 (그리고 어쩌면)을 변경하려고합니다. – dfd
하단 기능 '앵커'를보십시오. 그것이 제약 조건을 설정하는 방법입니다. –
했습니다. 아직도 힘든 시간을 보내고 있습니다. * 구체적으로 당신이하려는 것은 무엇입니까? * 구체적으로 * 무엇이 문제입니까? 그리고 다시, 무엇이 변화하고 있습니까? (더 나은, 당신은 내가 실제로 재현 할 수있는 뭔가를 줄 수 있습니까? 편집 : 제약 조건을 변경했습니다. 슬라이딩 메뉴, 방향 변경 등. 게시 된 코드의 아무 것도 의미가 없습니다! ** ** ** 귀하의' anchor' 함수는'isActive = true'로 설정합니다. "버튼을 눌렀을 때의 제약 조건 변경"이라는 질문 제목에 간단히 표시되지 않습니다. – dfd