mirror of
https://github.com/material-components/material-components-ios.git
synced 2026-02-20 08:27:32 +08:00
## Introduction Before we move container scheme to ready we need to have both the `colorScheme` and `typographyScheme` nonnull. By making colorScheme and typographyScheme of MDCContainerScheming nonnull, clients will reduce the amount of conditional checks required in their apps and reduce a category of potential errors (accidentally passing nil to a nonnull parameter). ## The problem Both `colorScheme` and `typographyScheme` were nullable in the container scheme making it harder for clients to use the container scheme throughout their app on non Material UIElements, i.e. UIView.backgroundColor ## The fix Make both of these properties nonnull. ## Additional work - [x] I had to update the swift examples to not be optional and I additionally removed the nil checks from the ObjC examples.
119 lines
4.2 KiB
Swift
119 lines
4.2 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
|
|
import MaterialComponents.MaterialDialogs
|
|
import MaterialComponentsBeta.MaterialButtons_Theming
|
|
import MaterialComponentsBeta.MaterialContainerScheme
|
|
import MaterialComponentsBeta.MaterialDialogs_Theming
|
|
|
|
class CustomShadowViewController: UIViewController {
|
|
|
|
let bodyLabel = UILabel()
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
view.backgroundColor = UIColor.white
|
|
view.layer.cornerRadius = 32.0
|
|
|
|
bodyLabel.text =
|
|
"This presented view has a corner radius so we've set the corner radius on the presentation controller."
|
|
bodyLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
bodyLabel.numberOfLines = 0
|
|
bodyLabel.sizeToFit()
|
|
self.view.addSubview(bodyLabel)
|
|
|
|
NSLayoutConstraint.activate(
|
|
NSLayoutConstraint.constraints(withVisualFormat: "H:|-[body]-|", options: [], metrics: nil, views: [ "body": bodyLabel]))
|
|
NSLayoutConstraint.activate(
|
|
NSLayoutConstraint.constraints(withVisualFormat: "V:|-[body]-|", options: [], metrics: nil, views: [ "body": bodyLabel]))
|
|
}
|
|
|
|
override var preferredContentSize: CGSize {
|
|
get {
|
|
return CGSize(width:200.0, height:140.0);
|
|
}
|
|
set {
|
|
super.preferredContentSize = newValue
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class DialogsCustomShadowExampleViewController: UIViewController {
|
|
|
|
let textButton = MDCButton()
|
|
let transitionController = MDCDialogTransitionController()
|
|
var containerScheme: MDCContainerScheming = MDCContainerScheme()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
view.backgroundColor = containerScheme.colorScheme.backgroundColor
|
|
|
|
textButton.setTitle("PRESENT ALERT", for: UIControlState())
|
|
textButton.setTitleColor(UIColor(white: 0.1, alpha:1), for: UIControlState())
|
|
textButton.sizeToFit()
|
|
textButton.translatesAutoresizingMaskIntoConstraints = false
|
|
textButton.addTarget(self, action: #selector(tap), for: .touchUpInside)
|
|
textButton.applyTextTheme(withScheme: containerScheme)
|
|
self.view.addSubview(textButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
NSLayoutConstraint(item:textButton,
|
|
attribute:.centerX,
|
|
relatedBy:.equal,
|
|
toItem:self.view,
|
|
attribute:.centerX,
|
|
multiplier:1.0,
|
|
constant: 0.0),
|
|
NSLayoutConstraint(item:textButton,
|
|
attribute:.centerY,
|
|
relatedBy:.equal,
|
|
toItem:self.view,
|
|
attribute:.centerY,
|
|
multiplier:1.0,
|
|
constant: 0.0)
|
|
])
|
|
}
|
|
|
|
@objc func tap(_ sender: Any) {
|
|
let presentedController = CustomShadowViewController(nibName: nil, bundle: nil)
|
|
presentedController.modalPresentationStyle = .custom;
|
|
presentedController.transitioningDelegate = self.transitionController;
|
|
|
|
self.present(presentedController, animated: true, completion: nil)
|
|
|
|
// We set the corner radius to adjust the shadow that is implemented via the trackingView in the
|
|
// presentation controller.
|
|
if let presentationController = presentedController.mdc_dialogPresentationController {
|
|
presentationController.applyTheme(withScheme: containerScheme)
|
|
presentationController.dialogCornerRadius = presentedController.view.layer.cornerRadius
|
|
}
|
|
}
|
|
|
|
// MARK: Catalog by convention
|
|
|
|
class func catalogMetadata() -> [String: Any] {
|
|
return [
|
|
"breadcrumbs": ["Dialogs", "View with Corner Radius"],
|
|
"primaryDemo": false,
|
|
"presentable": false,
|
|
]
|
|
}
|
|
}
|