在iOS的BottomAppBar中自定义FAB

时间:2018-11-11 18:14:13

标签: ios floating-action-button material-components-ios

只需阅读an article about customising BottomAppBar in Material Components for Android,现在我想知道如何针对iOS执行相同的操作。实际上,我需要更改fabCradleDiameter,fabCradleRoundedCornerRadius,fabCradleVerticalOffset和FAB半径。上面的文章显示,在Android上,它是通过一些布局xml中的app:*参数完成的。但是iOS中没有这样的xml。

1 个答案:

答案 0 :(得分:0)

希望在iOS触摸中执行该操作以在执行时创建组件,要执行此操作,您可以通过以下方式进行操作:

 var bottomAppBar: MDCBottomAppBarView {
            let bottomAppBar = MDCBottomAppBarView()
            // background color Bottom App Bar
            bottomAppBar.barTintColor = UIColor(named: "Teal")
            // Image floatingButton
            bottomAppBar.floatingButton.setImage(UIImage(named: "CloudUpload"), for: .normal)
            // Background color floatingButton
            bottomAppBar.floatingButton.backgroundColor = UIColor(named: "Gray600")
            // here you define the size of the bottom app bar, in my case I define the size based on a view that I added to the ViewController
            let size = bottomAppBar.sizeThatFits(self.containerView.bounds.size)
            bottomAppBar.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) 

            // The following lines of code are to define the buttons on the right and left side
            let barButtonLeadingItem = UIBarButtonItem(
                image: UIImage(named:"IconName"), // Icon
                style: .plain,
                target: self,
                action: #selector(self.onMenuButtonTapped))

            let barButtonTrailingItem = UIBarButtonItem(
                image: UIImage(named: "IconName"), // Icon
                style: .plain,
                target: self,
                action: #selector(self.onNavigationButtonTapped))
            bottomAppBar.leadingBarButtonItems = [barButtonLeadingItem]
            bottomAppBar.trailingBarButtonItems = [barButtonTrailingItem]
            return bottomAppBar
        }
 // Add bottombar to view
 self.containerView.addSubview(bottomAppBar)

注意::以上代码已在 Swift 5.0

中进行了测试