iOS 使用谷歌 MaterialComponents?

ary*_*axt 2 ios material-components

我正在尝试使用 MaterialComponents 中的 MDCRaisedButton,将其添加为与所有其他依赖项类似的 pod 依赖项,但是当我尝试导入它时,出现编译错误 Module MaterialComponents not found

使用这个 pod 需要采取什么额外的步骤吗?

我在他们使用的演示中注意到

  pod 'MaterialComponents/Typography', :path => '../../'
Run Code Online (Sandbox Code Playgroud)

路径有什么作用?当我尝试运行 pod update 时出现错误

Yar*_*neo 5

:path不是MaterialComponents在您自己的应用程序中使用时需要的附加功能,而是用于在本地 pod 之上进行开发。在这里查看更多信息:https : //guides.cocoapods.org/using/the-podfile.html#using-the-files-from-a-folder-local-to-the-machine

为了使用,MDCRaisedButton您首先需要pod 'MaterialComponents/Buttons'在所选的应用程序目标内部创建一个 Podfile 。如果您使用 swift 作为开发语言,我建议还添加use_frameworks!. Podfile 示例如下所示:

target 'Demo App' do
  use_frameworks! # can remove if using Objective-C
  pod 'MaterialComponents/Buttons'
end
Run Code Online (Sandbox Code Playgroud)

之后,导入和使用将是:

迅速:

import MaterialComponents.MaterialButtons

let button = MDCRaisedButton()
Run Code Online (Sandbox Code Playgroud)

目标-C:

#import "MaterialButtons.h"

MDCRaisedButton *button = [[MDCRaisedButton alloc] init];
Run Code Online (Sandbox Code Playgroud)

更多信息可以在这里找到:https : //github.com/material-components/material-components-ios/tree/develop/components/Buttons


作为旁注,MDCRaisedButton将很快被弃用,并且MDCButton使用主题化MDCContainedButtonThemer现在是获得相同凸起按钮样式的最佳方式。因此,当前执行此操作的最佳实践是添加到您的 podfile 中:

pod 'MaterialComponents/Buttons+Extensions/ButtonThemer'

然后在你的实现中:

迅速:

import MaterialComponents.MaterialButtons_ButtonThemer

let buttonScheme = MDCButtonScheme()
let button = MDCButton()
MDCContainedButtonThemer.applyScheme(buttonScheme, to: button)
Run Code Online (Sandbox Code Playgroud)

目标-C:

#import "MaterialButtons+ButtonThemer.h"

MDCButton *button = [[MDCButton alloc] init];
MDCButtonScheme *buttonScheme = [[MDCButtonScheme alloc] init];
[MDCContainedButtonThemer applyScheme:buttonScheme toButton:button];
Run Code Online (Sandbox Code Playgroud)

更多信息可以在这里找到:https : //github.com/material-components/material-components-ios/blob/develop/components/Buttons/docs/theming.md

使用主题和方案的附加价值是您可以自定义按钮方案并让该方案一次应用于所有按钮。此外,如果您希望在整个应用程序中使用特定的配色方案和/或排版方案,主题现在允许将这些方案应用于应用程序中的所有材料组件。