How to use traitCollectionDidChange method of CancellingWKWebViewNavigationDelegate class

Best Swift-snapshot-testing code snippet using CancellingWKWebViewNavigationDelegate.traitCollectionDidChange

SnapshotTestingTests.swift

Source:SnapshotTestingTests.swift Github

copy

Full Screen

...392 self.bottomLabel.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),393 self.bottomLabel.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),394 ])395 }396 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {397 super.traitCollectionDidChange(previousTraitCollection)398 self.topLabel.font = .preferredFont(forTextStyle: .headline, compatibleWith: self.traitCollection)399 self.leadingLabel.font = .preferredFont(forTextStyle: .body, compatibleWith: self.traitCollection)400 self.trailingLabel.font = .preferredFont(forTextStyle: .body, compatibleWith: self.traitCollection)401 self.bottomLabel.font = .preferredFont(forTextStyle: .subheadline, compatibleWith: self.traitCollection)402 self.view.setNeedsUpdateConstraints()403 self.view.updateConstraintsIfNeeded()404 }405 }406 let viewController = MyViewController()407 #if os(iOS)408 assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe), named: "iphone-se")409 assertSnapshot(matching: viewController, as: .image(on: .iPhone8), named: "iphone-8")410 assertSnapshot(matching: viewController, as: .image(on: .iPhone8Plus), named: "iphone-8-plus")411 assertSnapshot(matching: viewController, as: .image(on: .iPhoneX), named: "iphone-x")412 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXr), named: "iphone-xr")413 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax), named: "iphone-xs-max")414 assertSnapshot(matching: viewController, as: .image(on: .iPadMini), named: "ipad-mini")415 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7), named: "ipad-9-7")416 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2), named: "ipad-10-2")417 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5), named: "ipad-pro-10-5")418 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11), named: "ipad-pro-11")419 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9), named: "ipad-pro-12-9")420 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhoneSe), named: "iphone-se")421 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhone8), named: "iphone-8")422 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhone8Plus), named: "iphone-8-plus")423 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhoneX), named: "iphone-x")424 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhoneXr), named: "iphone-xr")425 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPhoneXsMax), named: "iphone-xs-max")426 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPadMini), named: "ipad-mini")427 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPad9_7), named: "ipad-9-7")428 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPad10_2), named: "ipad-10-2")429 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPadPro10_5), named: "ipad-pro-10-5")430 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPadPro11), named: "ipad-pro-11")431 assertSnapshot(matching: viewController, as: .recursiveDescription(on: .iPadPro12_9), named: "ipad-pro-12-9")432 assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe(.portrait)), named: "iphone-se")433 assertSnapshot(matching: viewController, as: .image(on: .iPhone8(.portrait)), named: "iphone-8")434 assertSnapshot(matching: viewController, as: .image(on: .iPhone8Plus(.portrait)), named: "iphone-8-plus")435 assertSnapshot(matching: viewController, as: .image(on: .iPhoneX(.portrait)), named: "iphone-x")436 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXr(.portrait)), named: "iphone-xr")437 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax(.portrait)), named: "iphone-xs-max")438 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.landscape)), named: "ipad-mini")439 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.landscape)), named: "ipad-9-7")440 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.landscape)), named: "ipad-10-2")441 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.landscape)), named: "ipad-pro-10-5")442 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.landscape)), named: "ipad-pro-11")443 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.landscape)), named: "ipad-pro-12-9")444 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.landscape(splitView: .oneThird))), named: "ipad-mini-33-split-landscape")445 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.landscape(splitView: .oneHalf))), named: "ipad-mini-50-split-landscape")446 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.landscape(splitView: .twoThirds))), named: "ipad-mini-66-split-landscape")447 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.portrait(splitView: .oneThird))), named: "ipad-mini-33-split-portrait")448 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.portrait(splitView: .twoThirds))), named: "ipad-mini-66-split-portrait")449 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.landscape(splitView: .oneThird))), named: "ipad-9-7-33-split-landscape")450 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.landscape(splitView: .oneHalf))), named: "ipad-9-7-50-split-landscape")451 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.landscape(splitView: .twoThirds))), named: "ipad-9-7-66-split-landscape")452 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.portrait(splitView: .oneThird))), named: "ipad-9-7-33-split-portrait")453 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.portrait(splitView: .twoThirds))), named: "ipad-9-7-66-split-portrait")454 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.landscape(splitView: .oneThird))), named: "ipad-10-2-split-landscape")455 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.landscape(splitView: .oneHalf))), named: "ipad-10-2-50-split-landscape")456 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.landscape(splitView: .twoThirds))), named: "ipad-10-2-66-split-landscape")457 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.portrait(splitView: .oneThird))), named: "ipad-10-2-33-split-portrait")458 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.portrait(splitView: .twoThirds))), named: "ipad-10-2-66-split-portrait")459 460 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.landscape(splitView: .oneThird))), named: "ipad-pro-10inch-33-split-landscape")461 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.landscape(splitView: .oneHalf))), named: "ipad-pro-10inch-50-split-landscape")462 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.landscape(splitView: .twoThirds))), named: "ipad-pro-10inch-66-split-landscape")463 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.portrait(splitView: .oneThird))), named: "ipad-pro-10inch-33-split-portrait")464 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.portrait(splitView: .twoThirds))), named: "ipad-pro-10inch-66-split-portrait")465 466 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.landscape(splitView: .oneThird))), named: "ipad-pro-11inch-33-split-landscape")467 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.landscape(splitView: .oneHalf))), named: "ipad-pro-11inch-50-split-landscape")468 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.landscape(splitView: .twoThirds))), named: "ipad-pro-11inch-66-split-landscape")469 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.portrait(splitView: .oneThird))), named: "ipad-pro-11inch-33-split-portrait")470 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.portrait(splitView: .twoThirds))), named: "ipad-pro-11inch-66-split-portrait")471 472 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.landscape(splitView: .oneThird))), named: "ipad-pro-12inch-33-split-landscape")473 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.landscape(splitView: .oneHalf))), named: "ipad-pro-12inch-50-split-landscape")474 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.landscape(splitView: .twoThirds))), named: "ipad-pro-12inch-66-split-landscape")475 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.portrait(splitView: .oneThird))), named: "ipad-pro-12inch-33-split-portrait")476 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.portrait(splitView: .twoThirds))), named: "ipad-pro-12inch-66-split-portrait")477 478 assertSnapshot(479 matching: viewController, as: .image(on: .iPhoneSe(.landscape)), named: "iphone-se-alternative")480 assertSnapshot(481 matching: viewController, as: .image(on: .iPhone8(.landscape)), named: "iphone-8-alternative")482 assertSnapshot(483 matching: viewController, as: .image(on: .iPhone8Plus(.landscape)), named: "iphone-8-plus-alternative")484 assertSnapshot(485 matching: viewController, as: .image(on: .iPhoneX(.landscape)), named: "iphone-x-alternative")486 assertSnapshot(487 matching: viewController, as: .image(on: .iPhoneXr(.landscape)), named: "iphone-xr-alternative")488 assertSnapshot(489 matching: viewController, as: .image(on: .iPhoneXsMax(.landscape)), named: "iphone-xs-max-alternative")490 assertSnapshot(491 matching: viewController, as: .image(on: .iPadMini(.portrait)), named: "ipad-mini-alternative")492 assertSnapshot(493 matching: viewController, as: .image(on: .iPad9_7(.portrait)), named: "ipad-9-7-alternative")494 assertSnapshot(495 matching: viewController, as: .image(on: .iPad10_2(.portrait)), named: "ipad-10-2-alternative")496 assertSnapshot(497 matching: viewController, as: .image(on: .iPadPro10_5(.portrait)), named: "ipad-pro-10-5-alternative")498 assertSnapshot(499 matching: viewController, as: .image(on: .iPadPro11(.portrait)), named: "ipad-pro-11-alternative")500 assertSnapshot(501 matching: viewController, as: .image(on: .iPadPro12_9(.portrait)), named: "ipad-pro-12-9-alternative")502 allContentSizes.forEach { name, contentSize in503 assertSnapshot(504 matching: viewController,505 as: .image(on: .iPhoneSe, traits: .init(preferredContentSizeCategory: contentSize)),506 named: "iphone-se-\(name)"507 )508 }509 #elseif os(tvOS)510 assertSnapshot(511 matching: viewController, as: .image(on: .tv), named: "tv")512 assertSnapshot(513 matching: viewController, as: .image(on: .tv4K), named: "tv4k")514 #endif515 }516 #endif517 }518 func testTraitsEmbeddedInTabNavigation() {519 #if os(iOS)520 if #available(iOS 11.0, *) {521 class MyViewController: UIViewController {522 let topLabel = UILabel()523 let leadingLabel = UILabel()524 let trailingLabel = UILabel()525 let bottomLabel = UILabel()526 override func viewDidLoad() {527 super.viewDidLoad()528 self.navigationItem.leftBarButtonItem = .init(barButtonSystemItem: .add, target: nil, action: nil)529 self.view.backgroundColor = .white530 self.topLabel.text = "What's"531 self.leadingLabel.text = "the"532 self.trailingLabel.text = "point"533 self.bottomLabel.text = "?"534 self.topLabel.translatesAutoresizingMaskIntoConstraints = false535 self.leadingLabel.translatesAutoresizingMaskIntoConstraints = false536 self.trailingLabel.translatesAutoresizingMaskIntoConstraints = false537 self.bottomLabel.translatesAutoresizingMaskIntoConstraints = false538 self.view.addSubview(self.topLabel)539 self.view.addSubview(self.leadingLabel)540 self.view.addSubview(self.trailingLabel)541 self.view.addSubview(self.bottomLabel)542 NSLayoutConstraint.activate([543 self.topLabel.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),544 self.topLabel.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),545 self.leadingLabel.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),546 self.leadingLabel.trailingAnchor.constraint(lessThanOrEqualTo: self.view.safeAreaLayoutGuide.centerXAnchor),547// self.leadingLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),548 self.leadingLabel.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor),549 self.trailingLabel.leadingAnchor.constraint(greaterThanOrEqualTo: self.view.safeAreaLayoutGuide.centerXAnchor),550 self.trailingLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),551 self.trailingLabel.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor),552 self.bottomLabel.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),553 self.bottomLabel.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),554 ])555 }556 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {557 super.traitCollectionDidChange(previousTraitCollection)558 self.topLabel.font = .preferredFont(forTextStyle: .headline, compatibleWith: self.traitCollection)559 self.leadingLabel.font = .preferredFont(forTextStyle: .body, compatibleWith: self.traitCollection)560 self.trailingLabel.font = .preferredFont(forTextStyle: .body, compatibleWith: self.traitCollection)561 self.bottomLabel.font = .preferredFont(forTextStyle: .subheadline, compatibleWith: self.traitCollection)562 self.view.setNeedsUpdateConstraints()563 self.view.updateConstraintsIfNeeded()564 }565 }566 let myViewController = MyViewController()567 let navController = UINavigationController(rootViewController: myViewController)568 let viewController = UITabBarController()569 viewController.setViewControllers([navController], animated: false)570 assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe), named: "iphone-se")571 assertSnapshot(matching: viewController, as: .image(on: .iPhone8), named: "iphone-8")572 assertSnapshot(matching: viewController, as: .image(on: .iPhone8Plus), named: "iphone-8-plus")573 assertSnapshot(matching: viewController, as: .image(on: .iPhoneX), named: "iphone-x")574 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXr), named: "iphone-xr")575 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax), named: "iphone-xs-max")576 assertSnapshot(matching: viewController, as: .image(on: .iPadMini), named: "ipad-mini")577 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7), named: "ipad-9-7")578 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2), named: "ipad-10-2")579 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5), named: "ipad-pro-10-5")580 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11), named: "ipad-pro-11")581 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9), named: "ipad-pro-12-9")582 assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe(.portrait)), named: "iphone-se")583 assertSnapshot(matching: viewController, as: .image(on: .iPhone8(.portrait)), named: "iphone-8")584 assertSnapshot(matching: viewController, as: .image(on: .iPhone8Plus(.portrait)), named: "iphone-8-plus")585 assertSnapshot(matching: viewController, as: .image(on: .iPhoneX(.portrait)), named: "iphone-x")586 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXr(.portrait)), named: "iphone-xr")587 assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax(.portrait)), named: "iphone-xs-max")588 assertSnapshot(matching: viewController, as: .image(on: .iPadMini(.landscape)), named: "ipad-mini")589 assertSnapshot(matching: viewController, as: .image(on: .iPad9_7(.landscape)), named: "ipad-9-7")590 assertSnapshot(matching: viewController, as: .image(on: .iPad10_2(.landscape)), named: "ipad-10-2")591 assertSnapshot(matching: viewController, as: .image(on: .iPadPro10_5(.landscape)), named: "ipad-pro-10-5")592 assertSnapshot(matching: viewController, as: .image(on: .iPadPro11(.landscape)), named: "ipad-pro-11")593 assertSnapshot(matching: viewController, as: .image(on: .iPadPro12_9(.landscape)), named: "ipad-pro-12-9")594 assertSnapshot(595 matching: viewController, as: .image(on: .iPhoneSe(.landscape)), named: "iphone-se-alternative")596 assertSnapshot(597 matching: viewController, as: .image(on: .iPhone8(.landscape)), named: "iphone-8-alternative")598 assertSnapshot(599 matching: viewController, as: .image(on: .iPhone8Plus(.landscape)), named: "iphone-8-plus-alternative")600 assertSnapshot(601 matching: viewController, as: .image(on: .iPhoneX(.landscape)), named: "iphone-x-alternative")602 assertSnapshot(603 matching: viewController, as: .image(on: .iPhoneXr(.landscape)), named: "iphone-xr-alternative")604 assertSnapshot(605 matching: viewController, as: .image(on: .iPhoneXsMax(.landscape)), named: "iphone-xs-max-alternative")606 assertSnapshot(607 matching: viewController, as: .image(on: .iPadMini(.portrait)), named: "ipad-mini-alternative")608 assertSnapshot(609 matching: viewController, as: .image(on: .iPad9_7(.portrait)), named: "ipad-9-7-alternative")610 assertSnapshot(611 matching: viewController, as: .image(on: .iPad10_2(.portrait)), named: "ipad-10-2-alternative")612 assertSnapshot(613 matching: viewController, as: .image(on: .iPadPro10_5(.portrait)), named: "ipad-pro-10-5-alternative")614 assertSnapshot(615 matching: viewController, as: .image(on: .iPadPro11(.portrait)), named: "ipad-pro-11-alternative")616 assertSnapshot(617 matching: viewController, as: .image(on: .iPadPro12_9(.portrait)), named: "ipad-pro-12-9-alternative")618 }619 #endif620 }621 func testCollectionViewsWithMultipleScreenSizes() {622 #if os(iOS)623 final class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {624 let flowLayout: UICollectionViewFlowLayout = {625 let layout = UICollectionViewFlowLayout()626 layout.scrollDirection = .horizontal627 layout.minimumLineSpacing = 20628 return layout629 }()630 lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)631 override func viewDidLoad() {632 super.viewDidLoad()633 view.backgroundColor = .white634 view.addSubview(collectionView)635 collectionView.backgroundColor = .white636 collectionView.dataSource = self637 collectionView.delegate = self638 collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")639 collectionView.translatesAutoresizingMaskIntoConstraints = false640 NSLayoutConstraint.activate([641 collectionView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),642 collectionView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),643 collectionView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),644 collectionView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor)645 ])646 collectionView.reloadData()647 }648 override func viewDidLayoutSubviews() {649 super.viewDidLayoutSubviews()650 collectionView.collectionViewLayout.invalidateLayout()651 }652 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {653 super.traitCollectionDidChange(previousTraitCollection)654 collectionView.collectionViewLayout.invalidateLayout()655 }656 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {657 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)658 cell.contentView.backgroundColor = .orange659 return cell660 }661 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {662 return 20663 }664 func collectionView(665 _ collectionView: UICollectionView,666 layout collectionViewLayout: UICollectionViewLayout,667 sizeForItemAt indexPath: IndexPath...

Full Screen

Full Screen

SnapshotKitTests.swift

Source:SnapshotKitTests.swift Github

copy

Full Screen

...369 override func viewDidLayoutSubviews() {370 super.viewDidLayoutSubviews()371 collectionView.collectionViewLayout.invalidateLayout()372 }373 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {374 super.traitCollectionDidChange(previousTraitCollection)375 collectionView.collectionViewLayout.invalidateLayout()376 }377 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {378 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)379 cell.contentView.backgroundColor = .orange380 return cell381 }382 func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {383 20384 }385 func collectionView(386 _ collectionView: UICollectionView,387 layout _: UICollectionViewLayout,388 sizeForItemAt _: IndexPath389 ) -> CGSize {390 CGSize(391 width: min(collectionView.frame.width - 50, 300),392 height: collectionView.frame.height393 )394 }395 }396 let viewController = CollectionViewController()397 assertSnapshots(matching: viewController, as: [398 "ipad": .image(on: .iPadPro12_9),399 "iphoneSe": .image(on: .iPhoneSe),400 "iphone8": .image(on: .iPhone8),401 "iphoneMax": .image(on: .iPhoneXsMax)402 ])403 }404 func testUIView() {405 let view = UIView()406 view.frame = .init(origin: .zero, size: .init(width: 20, height: 20))407 view.backgroundColor = .cyan408 view.layer.cornerRadius = 10409 assertSnapshot(matching: view, as: .image)410 assertSnapshot(matching: view, as: .recursiveDescription)411 }412 func testUIViewController() {413 let view = UIView()414 view.backgroundColor = .cyan415 view.layer.cornerRadius = 10416 view.translatesAutoresizingMaskIntoConstraints = false417 let viewController = UIViewController()418 viewController.view.addSubview(view)419 NSLayoutConstraint.activate([420 view.topAnchor.constraint(equalTo: viewController.view.topAnchor),421 view.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor),422 view.leftAnchor.constraint(equalTo: viewController.view.leftAnchor),423 view.rightAnchor.constraint(equalTo: viewController.view.rightAnchor)424 ])425 assertSnapshot(matching: viewController, as: .image(size: .init(width: 100, height: 100)))426 }427 func testUIViewControllerLifeCycle() {428 class ViewController: UIViewController {429 let viewDidLoadExpectation: XCTestExpectation430 let viewWillAppearExpectation: XCTestExpectation431 let viewDidAppearExpectation: XCTestExpectation432 let viewWillDisappearExpectation: XCTestExpectation433 let viewDidDisappearExpectation: XCTestExpectation434 init(435 viewDidLoadExpectation: XCTestExpectation,436 viewWillAppearExpectation: XCTestExpectation,437 viewDidAppearExpectation: XCTestExpectation,438 viewWillDisappearExpectation: XCTestExpectation,439 viewDidDisappearExpectation: XCTestExpectation440 ) {441 self.viewDidLoadExpectation = viewDidLoadExpectation442 self.viewWillAppearExpectation = viewWillAppearExpectation443 self.viewDidAppearExpectation = viewDidAppearExpectation444 self.viewWillDisappearExpectation = viewWillDisappearExpectation445 self.viewDidDisappearExpectation = viewDidDisappearExpectation446 super.init(nibName: nil, bundle: nil)447 }448 @available(*, unavailable)449 required init?(coder _: NSCoder) {450 fatalError("init(coder:) has not been implemented")451 }452 override func viewDidLoad() {453 super.viewDidLoad()454 viewDidLoadExpectation.fulfill()455 }456 override func viewWillAppear(_ animated: Bool) {457 super.viewWillAppear(animated)458 viewWillAppearExpectation.fulfill()459 }460 override func viewDidAppear(_ animated: Bool) {461 super.viewDidAppear(animated)462 viewDidAppearExpectation.fulfill()463 }464 override func viewWillDisappear(_ animated: Bool) {465 super.viewWillDisappear(animated)466 viewWillDisappearExpectation.fulfill()467 }468 override func viewDidDisappear(_ animated: Bool) {469 super.viewDidDisappear(animated)470 viewDidDisappearExpectation.fulfill()471 }472 }473 let viewDidLoadExpectation = expectation(description: "viewDidLoad")474 let viewWillAppearExpectation = expectation(description: "viewWillAppear")475 let viewDidAppearExpectation = expectation(description: "viewDidAppear")476 let viewWillDisappearExpectation = expectation(description: "viewWillDisappear")477 let viewDidDisappearExpectation = expectation(description: "viewDidDisappear")478 viewWillAppearExpectation.expectedFulfillmentCount = 2479 viewDidAppearExpectation.expectedFulfillmentCount = 2480 viewWillDisappearExpectation.expectedFulfillmentCount = 2481 viewDidDisappearExpectation.expectedFulfillmentCount = 2482 let viewController = ViewController(483 viewDidLoadExpectation: viewDidLoadExpectation,484 viewWillAppearExpectation: viewWillAppearExpectation,485 viewDidAppearExpectation: viewDidAppearExpectation,486 viewWillDisappearExpectation: viewWillDisappearExpectation,487 viewDidDisappearExpectation: viewDidDisappearExpectation488 )489 assertSnapshot(matching: viewController, as: .image)490 assertSnapshot(matching: viewController, as: .image)491 wait(492 for: [493 viewDidLoadExpectation,494 viewWillAppearExpectation,495 viewDidAppearExpectation,496 viewWillDisappearExpectation,497 viewDidDisappearExpectation498 ],499 timeout: 1,500 enforceOrder: true501 )502 }503 func testCALayer() {504 let layer = CALayer()505 layer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)506 layer.backgroundColor = UIColor.red.cgColor507 layer.borderWidth = 4.0508 layer.borderColor = UIColor.black.cgColor509 assertSnapshot(matching: layer, as: .image)510 }511 func testCALayerWithGradient() {512 let baseLayer = CALayer()513 baseLayer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)514 let gradientLayer = CAGradientLayer()515 gradientLayer.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]516 gradientLayer.frame = baseLayer.frame517 baseLayer.addSublayer(gradientLayer)518 assertSnapshot(matching: baseLayer, as: .image(subpixelThreshold: 1))519 }520 func testViewControllerHierarchy() {521 let page = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)522 page.setViewControllers([UIViewController()], direction: .forward, animated: false)523 let tab = UITabBarController()524 tab.viewControllers = [525 UINavigationController(rootViewController: page),526 UINavigationController(rootViewController: UIViewController()),527 UINavigationController(rootViewController: UIViewController()),528 UINavigationController(rootViewController: UIViewController()),529 UINavigationController(rootViewController: UIViewController())530 ]531 assertSnapshot(matching: tab, as: .hierarchy)532 }533 #endif534 #if os(iOS) || os(tvOS)535 func testUIBezierPath() {536 let path = UIBezierPath.heart537 let osName: String538 #if os(iOS)539 osName = "iOS"540 #elseif os(tvOS)541 osName = "tvOS"542 #endif543 assertSnapshot(matching: path, as: .image, named: osName)544 assertSnapshot(matching: path, as: .elementsDescription, named: osName)545 }546 #endif547 func testURLRequest() {548 var get = URLRequest(url: URL(string: "https://www.pointfree.co/")!)549 get.addValue("pf_session={}", forHTTPHeaderField: "Cookie")550 get.addValue("text/html", forHTTPHeaderField: "Accept")551 get.addValue("application/json", forHTTPHeaderField: "Content-Type")552 assertSnapshot(matching: get, as: .raw, named: "get")553 assertSnapshot(matching: get, as: .curl, named: "get-curl")554 var getWithQuery = URLRequest(url: URL(string: "https://www.pointfree.co?key_2=value_2&key_1=value_1&key_3=value_3")!)555 getWithQuery.addValue("pf_session={}", forHTTPHeaderField: "Cookie")556 getWithQuery.addValue("text/html", forHTTPHeaderField: "Accept")557 getWithQuery.addValue("application/json", forHTTPHeaderField: "Content-Type")558 assertSnapshot(matching: getWithQuery, as: .raw, named: "get-with-query")559 assertSnapshot(matching: getWithQuery, as: .curl, named: "get-with-query-curl")560 var post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)561 post.httpMethod = "POST"562 post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")563 post.addValue("text/html", forHTTPHeaderField: "Accept")564 post.httpBody = Data("pricing[billing]=monthly&pricing[lane]=individual".utf8)565 assertSnapshot(matching: post, as: .raw, named: "post")566 assertSnapshot(matching: post, as: .curl, named: "post-curl")567 var postWithJSON = URLRequest(url: URL(string: "http://dummy.restapiexample.com/api/v1/create")!)568 postWithJSON.httpMethod = "POST"569 postWithJSON.addValue("application/json", forHTTPHeaderField: "Content-Type")570 postWithJSON.addValue("application/json", forHTTPHeaderField: "Accept")571 postWithJSON.httpBody = Data("{\"name\":\"tammy134235345235\", \"salary\":0, \"age\":\"tammy133\"}".utf8)572 assertSnapshot(matching: postWithJSON, as: .raw, named: "post-with-json")573 assertSnapshot(matching: postWithJSON, as: .curl, named: "post-with-json-curl")574 var head = URLRequest(url: URL(string: "https://www.pointfree.co/")!)575 head.httpMethod = "HEAD"576 head.addValue("pf_session={}", forHTTPHeaderField: "Cookie")577 assertSnapshot(matching: head, as: .raw, named: "head")578 assertSnapshot(matching: head, as: .curl, named: "head-curl")579 post = URLRequest(url: URL(string: "https://www.pointfree.co/subscribe")!)580 post.httpMethod = "POST"581 post.addValue("pf_session={\"user_id\":\"0\"}", forHTTPHeaderField: "Cookie")582 post.addValue("application/json", forHTTPHeaderField: "Accept")583 post.httpBody = Data("""584 {"pricing": {"lane": "individual","billing": "monthly"}}585 """.utf8)586 _assertInlineSnapshot(matching: post, as: .raw(pretty: true), with: """587 POST https://www.pointfree.co/subscribe588 Accept: application/json589 Cookie: pf_session={"user_id":"0"}590 {591 "pricing" : {592 "billing" : "monthly",593 "lane" : "individual"594 }595 }596 """)597 }598 #if os(iOS) || os(macOS)599 func testWebView() throws {600 let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)601 .deletingLastPathComponent()602 .appendingPathComponent("__Fixtures__/pointfree.html")603 let html = try String(contentsOf: fixtureUrl)604 let webView = WKWebView()605 webView.loadHTMLString(html, baseURL: nil)606 assertSnapshot(607 matching: webView,608 as: .image(precision: 0.9, size: .init(width: 800, height: 600)),609 named: platform610 )611 }612 #endif613 #if os(iOS) || os(tvOS)614 func testViewWithZeroHeightOrWidth() {615 var rect = CGRect(x: 0, y: 0, width: 350, height: 0)616 var view = UIView(frame: rect)617 view.backgroundColor = .red618 assertSnapshot(matching: view, as: .image, named: "noHeight")619 rect = CGRect(x: 0, y: 0, width: 0, height: 350)620 view = UIView(frame: rect)621 view.backgroundColor = .green622 assertSnapshot(matching: view, as: .image, named: "noWidth")623 rect = CGRect(x: 0, y: 0, width: 0, height: 0)624 view = UIView(frame: rect)625 view.backgroundColor = .blue626 assertSnapshot(matching: view, as: .image, named: "noWidth.noHeight")627 }628 #endif629 #if os(iOS)630 func testEmbeddedWebView() throws {631 let label = UILabel()632 label.text = "Hello, Blob!"633 let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)634 .deletingLastPathComponent()635 .appendingPathComponent("__Fixtures__/pointfree.html")636 let html = try String(contentsOf: fixtureUrl)637 let webView = WKWebView()638 webView.loadHTMLString(html, baseURL: nil)639 webView.isHidden = true640 let stackView = UIStackView(arrangedSubviews: [label, webView])641 stackView.axis = .vertical642 assertSnapshot(643 matching: stackView,644 as: .image(size: .init(width: 800, height: 600)),645 named: platform646 )647 }648 #endif649 #if os(iOS) || os(macOS)650 final class ManipulatingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {651 func webView(_ webView: WKWebView, didFinish _: WKNavigation!) {652 webView.evaluateJavaScript("document.body.children[0].classList.remove(\"hero\")") // Change layout653 }654 }655 func testWebViewWithManipulatingNavigationDelegate() throws {656 let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate()657 let webView = WKWebView()658 webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate659 let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)660 .deletingLastPathComponent()661 .appendingPathComponent("__Fixtures__/pointfree.html")662 let html = try String(contentsOf: fixtureUrl)663 webView.loadHTMLString(html, baseURL: nil)664 assertSnapshot(665 matching: webView,666 as: .image(precision: 0.9, size: .init(width: 800, height: 600)),667 named: platform668 )669 _ = manipulatingWKWebViewNavigationDelegate670 }671 final class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {672 func webView(673 _: WKWebView,674 decidePolicyFor _: WKNavigationAction,675 decisionHandler: @escaping (WKNavigationActionPolicy) -> Void676 ) {677 decisionHandler(.cancel)678 }679 }680 func testWebViewWithCancellingNavigationDelegate() throws {681 let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate()682 let webView = WKWebView()683 webView.navigationDelegate = cancellingWKWebViewNavigationDelegate684 let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)685 .deletingLastPathComponent()686 .appendingPathComponent("__Fixtures__/pointfree.html")687 let html = try String(contentsOf: fixtureUrl)688 webView.loadHTMLString(html, baseURL: nil)689 assertSnapshot(690 matching: webView,691 as: .image(size: .init(width: 800, height: 600)),692 named: platform693 )694 _ = cancellingWKWebViewNavigationDelegate695 }696 #endif697 #if os(iOS)698 @available(iOS 13.0, *)699 func testSwiftUIView_iOS() {700 let view = TestView().environment(\.colorScheme, .light)701 let precision: Float = 0.99702 assertSnapshot(matching: view, as: .image(precision: precision, traits: .init(userInterfaceStyle: .light)))703 assertSnapshot(matching: view, as: .image(precision: precision, layout: .sizeThatFits, traits: .init(userInterfaceStyle: .light)), named: "size-that-fits")704 assertSnapshot(matching: view, as: .image(precision: precision, layout: .fixed(width: 200, height: 100), traits: .init(userInterfaceStyle: .light)), named: "fixed")705 assertSnapshot(matching: view, as: .image(precision: precision, layout: .device(config: .iPhoneSe), traits: .init(userInterfaceStyle: .light)), named: "device")706 }707 #endif708 #if os(macOS)709 @available(macOS 11.0, *)710 func testSwiftUIView_macOS() {711 let view = TestView().environment(\.colorScheme, .light)712 assertSnapshot(matching: view, as: .image(size: .init(width: 100, height: 50), precision: 0.98))713 }714 #endif715 #if os(tvOS)716 @available(tvOS 13.0, *)717 func testSwiftUIView_tvOS() {718 let view = TestView().environment(\.colorScheme, .light)719 let precision: Float = 0.98720 assertSnapshot(matching: view, as: .image(precision: precision))721 assertSnapshot(matching: view, as: .image(precision: precision, layout: .sizeThatFits), named: "size-that-fits")722 assertSnapshot(matching: view, as: .image(precision: precision, layout: .fixed(width: 300, height: 100)), named: "fixed")723 assertSnapshot(matching: view, as: .image(precision: precision, layout: .device(config: .tv)), named: "device")724 }725 #endif726}727#if os(iOS) || os(tvOS)728class TestViewController: UIViewController {729 override func viewDidLoad() {730 super.viewDidLoad()731 let topView = UIView()732 let leadingView = UIView()733 let trailingView = UIView()734 let bottomView = UIView()735 navigationItem.leftBarButtonItem = .init(barButtonSystemItem: .add, target: nil, action: nil)736 view.backgroundColor = .white737 topView.backgroundColor = .blue738 leadingView.backgroundColor = .green739 trailingView.backgroundColor = .red740 bottomView.backgroundColor = .magenta741 topView.translatesAutoresizingMaskIntoConstraints = false742 leadingView.translatesAutoresizingMaskIntoConstraints = false743 trailingView.translatesAutoresizingMaskIntoConstraints = false744 bottomView.translatesAutoresizingMaskIntoConstraints = false745 view.addSubview(topView)746 view.addSubview(leadingView)747 view.addSubview(trailingView)748 view.addSubview(bottomView)749 let constant: CGFloat = 50750 NSLayoutConstraint.activate([751 topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),752 topView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),753 topView.widthAnchor.constraint(equalToConstant: constant),754 topView.heightAnchor.constraint(equalToConstant: constant),755 leadingView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),756 leadingView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor),757 leadingView.widthAnchor.constraint(equalToConstant: constant),758 leadingView.heightAnchor.constraint(equalToConstant: constant),759 trailingView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),760 trailingView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor),761 trailingView.widthAnchor.constraint(equalToConstant: constant),762 trailingView.heightAnchor.constraint(equalToConstant: constant),763 bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),764 bottomView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),765 bottomView.widthAnchor.constraint(equalToConstant: constant),766 bottomView.heightAnchor.constraint(equalToConstant: constant)767 ])768 }769 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {770 super.traitCollectionDidChange(previousTraitCollection)771 view.setNeedsUpdateConstraints()772 view.updateConstraintsIfNeeded()773 }774}775#endif776#if canImport(SwiftUI)777@available(iOS 13.0, macOS 11.0, tvOS 13.0, *)778private struct TestView: SwiftUI.View {779 var body: some SwiftUI.View {780 HStack {781 SwiftUI.Image(systemName: "checkmark.circle.fill")782 Text("Checked").fixedSize()783 }784 .padding(5)...

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {2 super.traitCollectionDidChange(previousTraitCollection)3 if traitCollection.forceTouchCapability == .available {4 registerForPreviewing(with: self, sourceView: view)5 }6}7override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {8 super.traitCollectionDidChange(previousTraitCollection)9 if traitCollection.forceTouchCapability == .available {10 registerForPreviewing(with: self, sourceView: view)11 }12}13override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {14 super.traitCollectionDidChange(previousTraitCollection)15 if traitCollection.forceTouchCapability == .available {16 registerForPreviewing(with: self, sourceView: view)17 }18}19override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {20 super.traitCollectionDidChange(previousTraitCollection)21 if traitCollection.forceTouchCapability == .available {22 registerForPreviewing(with: self, sourceView: view)23 }24}25override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {26 super.traitCollectionDidChange(previousTraitCollection)27 if traitCollection.forceTouchCapability == .available {28 registerForPreviewing(with: self, sourceView: view)29 }30}31override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {32 super.traitCollectionDidChange(previousTraitCollection)33 if traitCollection.forceTouchCapability == .available {34 registerForPreviewing(with: self, sourceView: view)35 }36}37override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {38 super.traitCollectionDidChange(previousTraitCollection)39 if traitCollection.forceTouchCapability == .available {40 registerForPreviewing(with: self, sourceView: view)41 }42}

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1import UIKit2import WebKit3class ViewController: UIViewController {4 override func viewDidLoad() {5 super.viewDidLoad()6 delegate = CancellingWKWebViewNavigationDelegate()7 }8 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {9 super.traitCollectionDidChange(previousTraitCollection)10 if traitCollection.userInterfaceStyle == .dark {11 delegate.cancel()12 }13 }14}15import Foundation16import WebKit17class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {18 func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {19 print("didStartProvisionalNavigation")20 }21 func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {22 print("didCommit")23 }24 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {25 print("didFinish")26 }27 func cancel() {28 print("cancel")29 }30}

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {2 super.traitCollectionDidChange(previousTraitCollection)3 if traitCollection.userInterfaceStyle == .dark {4 } else {5 }6}7override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {8 super.traitCollectionDidChange(previousTraitCollection)9 if traitCollection.userInterfaceStyle == .dark {10 } else {11 }12}13override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {14 super.traitCollectionDidChange(previousTraitCollection)15 if traitCollection.userInterfaceStyle == .dark {16 } else {17 }18}19override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {20 super.traitCollectionDidChange(previousTraitCollection)21 if traitCollection.userInterfaceStyle == .dark {22 } else {23 }24}25override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {26 super.traitCollectionDidChange(previousTraitCollection)27 if traitCollection.userInterfaceStyle == .dark {28 } else {29 }30}31override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {32 super.traitCollectionDidChange(previousTraitCollection)33 if traitCollection.userInterfaceStyle == .dark {34 } else {35 }36}37override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {38 super.traitCollectionDidChange(previousTraitCollection)39 if traitCollection.userInterfaceStyle == .dark {

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {2 super.traitCollectionDidChange(previousTraitCollection)3 guard let previousTraitCollection = previousTraitCollection else { return }4 guard let currentTraitCollection = self.traitCollection else { return }5 if previousTraitCollection.forceTouchCapability != currentTraitCollection.forceTouchCapability {6 switch currentTraitCollection.forceTouchCapability {7 print("Force Touch available")8 print("Force Touch not available")9 print("Force Touch unknown")10 }11 }12}13override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {14 super.traitCollectionDidChange(previousTraitCollection)15 guard let previousTraitCollection = previousTraitCollection else { return }16 guard let currentTraitCollection = self.traitCollection else { return }17 if previousTraitCollection.forceTouchCapability != currentTraitCollection.forceTouchCapability {18 switch currentTraitCollection.forceTouchCapability {19 print("Force Touch available")20 print("Force Touch not available")21 print("Force Touch unknown")22 }23 }24}25override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {26 super.traitCollectionDidChange(previousTraitCollection)27 guard let previousTraitCollection = previousTraitCollection else { return }28 guard let currentTraitCollection = self.traitCollection else { return }29 if previousTraitCollection.forceTouchCapability != currentTraitCollection.forceTouchCapability {30 switch currentTraitCollection.forceTouchCapability {31 print("Force Touch available")32 print("Force Touch not available")33 print("Force Touch unknown")34 }35 }36}37override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {38 super.traitCollectionDidChange(previousTraitCollection)39 guard let previousTraitCollection = previousTraitCollection else { return }40 guard let currentTraitCollection = self.traitCollection else { return }

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1class ViewController: UIViewController {2 override func viewDidLoad() {3 super.viewDidLoad()4 let webview = CancellingWKWebViewNavigationDelegate()5 self.view.addSubview(webview)6 }7}8class CancellingWKWebViewNavigationDelegate: WKWebView, WKNavigationDelegate {9 override init(frame: CGRect, configuration: WKWebViewConfiguration) {10 super.init(frame: frame, configuration: configuration)11 }12 required init?(coder: NSCoder) {13 super.init(coder: coder)14 }15 func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {16 print("didStartProvisionalNavigation")17 }18 func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {19 print("didReceiveServerRedirectForProvisionalNavigation")20 }21 func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {22 print("didFailProvisionalNavigation")23 }24 func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {25 print("didCommit")26 }27 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {28 print("didFinish")29 }30 func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {31 print("didFail")32 }33 func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {34 print("webViewWebContentProcessDidTerminate")35 }36 func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {37 print("didReceive")38 }39}40I have a WKWebView with a custom WKNavigationDelegate. I want to display a loading indicator when the WKWebView is loading content. I have tried to achieve this by implementing the webView(webView: WKWebView

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {2 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {3 super.traitCollectionDidChange(previousTraitCollection)4 }5 }6 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {7 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {8 super.traitCollectionDidChange(previousTraitCollection)9 }10 }11 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {12 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {13 super.traitCollectionDidChange(previousTraitCollection)14 }15 }16 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {17 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {18 super.traitCollectionDidChange(previousTraitCollection)19 }20 }21 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {22 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {23 super.traitCollectionDidChange(previousTraitCollection)24 }25 }26 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {27 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {28 super.traitCollectionDidChange(previousTraitCollection)29 }30 }31 class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {32 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {33 super.traitCollectionDidChange(previousTraitCollection)

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1class MyViewController: UIViewController {2 override func viewDidLoad() {3 super.viewDidLoad()4 let webViewConfig = WKWebViewConfiguration()5 webViewConfig.websiteDataStore = WKWebsiteDataStore.nonPersistent()6 webView = WKWebView(frame: CGRect.zero, configuration: webViewConfig)7 webView.navigationDelegate = CancellingWKWebViewNavigationDelegate()8 view.addSubview(webView)9 }10 override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {11 super.traitCollectionDidChange(previousTraitCollection)12 if previousTraitCollection?.preferredContentSizeCategory != traitCollection.preferredContentSizeCategory {13 }14 }15}16class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {17 func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {18 decisionHandler(.cancel)19 }20}

Full Screen

Full Screen

traitCollectionDidChange

Using AI Code Generation

copy

Full Screen

1import UIKit2import WebKit3class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {4 override func viewDidLoad() {5 super.viewDidLoad()6 webConfig = WKWebViewConfiguration()7 webViewNavigationDelegate = CancellingWKWebViewNavigationDelegate()8 webView = WKWebView(frame: view.bounds, configuration: webConfig)9 view.addSubview(webView)10 let request = URLRequest(url: url!)11 webView.load(request)12 }13}14class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {15 func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {16 decisionHandler(isCancelled ? .cancel : .allow)17 }18 func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {19 decisionHandler(isCancelled ? .cancel : .allow)20 }21}22import UIKit23import WebKit24class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {25 override func viewDidLoad() {26 super.viewDidLoad()27 webConfig = WKWebViewConfiguration()28 webViewNavigationDelegate = CancellingWKWebViewNavigationDelegate()29 webView = WKWebView(frame: view.bounds, configuration: webConfig)30 view.addSubview(webView)31 let request = URLRequest(url: url!)32 webView.load(request)33 }34 override func traitCollectionDidChange(_ previousTraitCollection: UIT

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful