material-components_materia.../components/Cards/examples/CardExampleViewController.swift
featherless be33d3196a
[Cards] Fix bug in the "Typical use" example where the image would not appear. (#8943)
The image was not appearing because the card using a custom image view that was trying to inherit the mask of the card's shape layer.

This change removes the custom image view and instead applies custom corner radii to the image view directly. This is not ideal, but is an improvement. Ideally, the card would provide a content view that is clipped to the card's shape. It's not possible to enable clipping on the card itself because this causes the shadow to go away.

| Before | After |
|:--|:--|
| ![Simulator Screen Shot - iPhone Xʀ - 2019-11-15 at 12 57 37](https://user-images.githubusercontent.com/45670/68964703-08b1ed80-07a8-11ea-978c-2c71145ca8c3.png) | ![Simulator Screen Shot - iPhone Xʀ - 2019-11-15 at 12 56 52](https://user-images.githubusercontent.com/45670/68964693-02bc0c80-07a8-11ea-9d3d-644b81aebee6.png) |

Part of https://github.com/material-components/material-components-ios/issues/8861
2019-11-15 16:44:28 -05:00

78 lines
2.8 KiB
Swift

// Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import UIKit
import MaterialComponents.MaterialButtons_ButtonThemer
import MaterialComponents.MaterialContainerScheme
import MaterialComponents.MaterialCards_Theming
import MaterialComponents.MaterialButtons_Theming
class CardExampleViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var card: MDCCard!
@IBOutlet weak var button: MDCButton!
@objc var containerScheme: MDCContainerScheming
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
containerScheme = MDCContainerScheme()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// The main image from the xib is taken from: https://unsplash.com/photos/wMzx2nBdeng
// License details: https://unsplash.com/license
let bundle = Bundle(for: CardExampleViewController.self)
bundle.loadNibNamed("CardExampleViewController", owner: self, options: nil)
view.frame = self.view.bounds
button.applyTextTheme(withScheme: containerScheme)
card.applyTheme(withScheme: containerScheme)
card.isInteractable = false
imageView.isAccessibilityElement = true
imageView.accessibilityLabel = "Missing Dish"
imageView.layer.cornerRadius = card.layer.cornerRadius
if #available(iOS 11.0, *) {
imageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}
}
override public var traitCollection: UITraitCollection {
if UIDevice.current.userInterfaceIdiom == .pad && UIDevice.current.orientation.isPortrait {
return UITraitCollection(traitsFrom:[UITraitCollection(horizontalSizeClass: .compact),
UITraitCollection(verticalSizeClass: .regular)])
}
return super.traitCollection
}
}
extension CardExampleViewController {
@objc class func catalogMetadata() -> [String: Any] {
return [
"breadcrumbs": ["Cards", "Card (Swift)"],
"description": "Cards contain content and actions about a single subject.",
"primaryDemo": true,
"presentable": true,
]
}
}