I’m making a group view that when one of many cells is pressed it navigates to a second view, nonetheless I’m experiencing a bug. When in Panorama orientation after I navigate to the second view the constraints change for the primary view.
That is what it appears like as vertical and panorama when the app is first open,
Now when ever any of those cells are pressed whereas in LANDSCAPE orientation it takes me to a brand new view (which is ok), however earlier than the transition finishes, I can see the constraints of the gathering view change (which isn’t) so I’m going again to the primary view and it appears totally different, it now appears like this:
I’m not certain what I’m doing mistaken. I attempted to not use safeAreaLayoutGuide however it makes no distinction to this drawback.
Right here is the code for the ViewController:
class ProfileViewController: UIViewController {
var profiles: [Profile] = []
personal let collectionView: UICollectionView = {
let viewLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(body: .zero, collectionViewLayout: viewLayout)
collectionView.backgroundColor = .white
return collectionView
}()
personal enum LayoutConstant {
static let spacing: CGFloat = 16.0
static let itemHeight: CGFloat = 300.0
}
override func viewDidLoad() {
tremendous.viewDidLoad()
setupViews()
setupLayouts()
populateProfiles()
collectionView.reloadData()
}
personal func setupViews() {
view.backgroundColor = .white
view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(ProfileCell.self, forCellWithReuseIdentifier: ProfileCell.identifier)
}
personal func setupLayouts() {
collectionView.translatesAutoresizingMaskIntoConstraints = false
// Format constraints for `collectionView`
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor)
])
}
personal func populateProfiles() {
profiles = [
Profile(name: "Thor", location: "Boston", imageName: "", profession: "astronomy"),
Profile(name: "Mike", location: "Albequerque", imageName: "", profession: "basketball"),
Profile(name: "Walter White", location: "New Mexico", imageName: "", profession: "chemistry"),
Profile(name: "Sam Brothers", location: "California", imageName: "", profession: "geography"),
Profile(name: "Chopin", location: "Norway", imageName: "", profession: "geometry"),
Profile(name: "Castles", location: "UK", imageName: "", profession: "history"),
Profile(name: "Dr. Johnson", location: "Australia", imageName: "", profession: "microscope"),
Profile(name: "Tom Hanks", location: "Bel Air", imageName: "", profession: "theater"),
Profile(name: "Roger Federer", location: "Switzerland", imageName: "", profession: "trophy"),
Profile(name: "Elon Musk", location: "San Francisco", imageName: "", profession: "graduate")
]
}
init() {
tremendous.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
}
extension ProfileViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return profiles.depend
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProfileCell.identifier, for: indexPath) as! ProfileCell
let profile = profiles[indexPath.row]
cell.setup(with: profile)
cell.contentView.backgroundColor = .crimson
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = LightViewController()
vc.modalPresentationStyle = .fullScreen
self.current(vc, animated: true)
var pressedData = Information(id: 1, title: "Check", description: "Check")
vc.viewModel.lightPublisher.ship(pressedData)
}
}
extension ProfileViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = itemWidth(for: view.body.width, spacing: LayoutConstant.spacing)
return CGSize(width: width, top: LayoutConstant.itemHeight)
}
func itemWidth(for width: CGFloat, spacing: CGFloat) -> CGFloat {
let itemsInRow: CGFloat = 2
let totalSpacing: CGFloat = 2 * spacing + (itemsInRow - 1) * spacing
let finalWidth = (width - totalSpacing) / itemsInRow
return flooring(finalWidth)
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, insetForSectionAt part: Int) -> UIEdgeInsets {
return UIEdgeInsets(prime: LayoutConstant.spacing, left: LayoutConstant.spacing, backside: LayoutConstant.spacing, proper: LayoutConstant.spacing)
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
return LayoutConstant.spacing
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
return LayoutConstant.spacing
}
}
that is the code for the cell:
import UIKit
protocol ReusableView: AnyObject {
static var identifier: String { get }
}
last class ProfileCell: UICollectionViewCell {
personal enum Constants {
// MARK: contentView structure constants
static let contentViewCornerRadius: CGFloat = 4.0
// MARK: profileImageView structure constants
static let imageHeight: CGFloat = 180.0
// MARK: Generic structure constants
static let verticalSpacing: CGFloat = 8.0
static let horizontalPadding: CGFloat = 16.0
static let profileDescriptionVerticalPadding: CGFloat = 8.0
}
personal let profileImageView: UIImageView = {
let imageView = UIImageView(body: .zero)
imageView.contentMode = .scaleAspectFill
return imageView
}()
let title: UILabel = {
let label = UILabel(body: .zero)
label.textAlignment = .middle
label.numberOfLines = 0
return label
}()
let locationLabel: UILabel = {
let label = UILabel(body: .zero)
label.textAlignment = .middle
label.numberOfLines = 0
return label
}()
let professionLabel: UILabel = {
let label = UILabel(body: .zero)
label.textAlignment = .middle
return label
}()
override init(body: CGRect) {
tremendous.init(body: .zero)
setupViews()
setupLayouts()
}
personal func setupViews() {
contentView.clipsToBounds = true
contentView.layer.cornerRadius = Constants.contentViewCornerRadius
contentView.backgroundColor = .white
contentView.addSubview(profileImageView)
contentView.addSubview(title)
contentView.addSubview(locationLabel)
contentView.addSubview(professionLabel)
}
personal func setupLayouts() {
profileImageView.translatesAutoresizingMaskIntoConstraints = false
title.translatesAutoresizingMaskIntoConstraints = false
locationLabel.translatesAutoresizingMaskIntoConstraints = false
professionLabel.translatesAutoresizingMaskIntoConstraints = false
// Format constraints for `profileImageView`
NSLayoutConstraint.activate([
profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
profileImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
profileImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
profileImageView.heightAnchor.constraint(equalToConstant: Constants.imageHeight)
])
// Format constraints for `usernameLabel`
NSLayoutConstraint.activate([
name.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: Constants.horizontalPadding),
name.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -Constants.horizontalPadding),
name.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: Constants.profileDescriptionVerticalPadding)
])
// Format constraints for `descriptionLabel`
NSLayoutConstraint.activate([
locationLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: Constants.horizontalPadding),
locationLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -Constants.horizontalPadding),
locationLabel.topAnchor.constraint(equalTo: name.bottomAnchor, constant: 4.0)
])
// Format constraints for `matchLabel`
NSLayoutConstraint.activate([
professionLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: Constants.horizontalPadding),
professionLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -Constants.horizontalPadding),
professionLabel.topAnchor.constraint(equalTo: locationLabel.bottomAnchor, constant: 8.0),
professionLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -Constants.profileDescriptionVerticalPadding)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
func setup(with profile: Profile) {
profileImageView.picture = UIImage(named: profile.imageName)
title.textual content = profile.title
locationLabel.textual content = profile.location
professionLabel.textual content = profile.occupation
}
}
extension ProfileCell: ReusableView {
static var identifier: String {
return String(describing: self)
}
}
If there’s anything I can reply please ask,
Thanks