Swift 5中如何调整Google Maps上的Floaty按钮及设置内边距?
iOS Floaty按钮内边距设置问题
我是iOS开发新手,已经创建了Floaty按钮,但尝试设置内边距始终无法生效,请问该如何设置?
现有代码
Floaty.global.rtlMode = true floaty.addItem("Start Navigation", icon: UIImage(named: "navigation-2 icon")!, handler: { item in let dialogMessage = UIAlertController(title: "Confirm Map Type", message: "Please select map Type for navigation", preferredStyle: .alert) // Create OK button with action handler let apple = UIAlertAction(title: "Apple Map", style: .default, handler: { (action) -> Void in print("Apple Map") self.openMapWithApple() }) // Create Cancel button with action handlder let google = UIAlertAction(title: "Google Map", style: .cancel) { (action) -> Void in print("Google Map") self.openMapWithgoogle() } //Add OK and Cancel button to an Alert object dialogMessage.addAction(apple) dialogMessage.addAction(google) // Present alert message to user self.present(dialogMessage, animated: true, completion: nil) floaty.close() }) self.view.addSubview(floaty)
解决方案
Floaty库提供了专门的属性来控制内边距,你可以在添加子项或把按钮添加到视图前设置这些属性:
设置主悬浮按钮的内边距
使用buttonInsets属性调整主按钮内部图标/内容的边距:// 示例:给主按钮设置8pt的内边距 floaty.buttonInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)设置展开后子项的内边距
使用itemInsets属性调整每个展开子项的整体内边距:// 示例:给子项设置上下4pt、左右8pt的内边距 floaty.itemInsets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)调整子项标题与图标的间距
如果需要单独调整子项内标题和图标的距离,可以使用itemTitlePadding:floaty.itemTitlePadding = 12 // 设置标题与图标之间的间距为12pt
修改后的完整代码示例
Floaty.global.rtlMode = true // 设置内边距属性 floaty.buttonInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8) floaty.itemInsets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8) floaty.itemTitlePadding = 12 floaty.addItem("Start Navigation", icon: UIImage(named: "navigation-2 icon")!, handler: { item in let dialogMessage = UIAlertController(title: "Confirm Map Type", message: "Please select map Type for navigation", preferredStyle: .alert) let apple = UIAlertAction(title: "Apple Map", style: .default, handler: { (action) -> Void in print("Apple Map") self.openMapWithApple() }) let google = UIAlertAction(title: "Google Map", style: .cancel) { (action) -> Void in print("Google Map") self.openMapWithgoogle() } dialogMessage.addAction(apple) dialogMessage.addAction(google) self.present(dialogMessage, animated: true, completion: nil) floaty.close() }) self.view.addSubview(floaty)
内容的提问来源于stack exchange,提问作者Muhammad Mustafeez Ur Rehman
相关产品推荐
相关产品推荐

