我需要创建自定义tabBar不规则形状编程。我发现了很多决策,但它们都与Interface Builder相连。代码在下面。自定义tabBar的所有方法都不在调试时调用。
final class TabBar: UITabBarController {
var customTabBar = CustomizedTabBar()
override var tabBar: UITabBar {
return customTabBar
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .naviBarBlack
UITabBar.appearance().barTintColor = .naviBarBlack
UITabBar.appearance().clipsToBounds = false
tabBar.tintColor = .white
tabBar.itemPositioning = .centered
setupVCs()
}
func setupVCs() {
guard let homeUnselected = UIImage(named: "home-unselected"),
let homeSelected = UIImage(named: "home-selected"),
let likeUnselected = UIImage(named: "like-unselected"),
let likeSelected = UIImage(named:"like-selected") else {return}
self.viewControllers = [
createNavController(for: MainScreenViewController(),
image: homeUnselected,
selected: homeSelected),
createNavController(for: UIViewController(),
image: likeUnselected,
selected: likeSelected)
]
}
private func createNavController(for rootViewController: UIViewController,
image: UIImage,
selected: UIImage) -> UIViewController {
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = image
navController.tabBarItem.selectedImage = selected
return navController
}
}
class CustomizedTabBar: UITabBar {
private var shapeLayer: CALayer?
private func addShape() {
let shapeLayer = CAShapeLayer()
shapeLayer.path = createPath()
shapeLayer.strokeColor = UIColor.lightGray.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.lineWidth = 1.0
if let oldShapeLayer = self.shapeLayer {
self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
} else {
self.layer.insertSublayer(shapeLayer, at: 0)
}
self.shapeLayer = shapeLayer
}
override func draw(_ rect: CGRect) {
self.addShape()
}
func createPath() -> CGPath {
let height: CGFloat = 37.0
let path = UIBezierPath()
let centerWidth = self.frame.width / 2
path.move(to: CGPoint(x: 0, y: 0)) // start top left
path.addLine(to: CGPoint(x: (centerWidth - height * 2), y: 0)) // the beginning of the trough
// first curve down
path.addCurve(to: CGPoint(x: centerWidth, y: height),
controlPoint1: CGPoint(x: (centerWidth - 30), y: 0), controlPoint2: CGPoint(x: centerWidth - 35, y: height))
// second curve up
path.addCurve(to: CGPoint(x: (centerWidth + height * 2), y: 0),
controlPoint1: CGPoint(x: centerWidth + 35, y: height), controlPoint2: CGPoint(x: (centerWidth + 30), y: 0))
// complete the rect
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
path.addLine(to: CGPoint(x: 0, y: self.frame.height))
path.close()
return path.cgPath
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let buttonRadius: CGFloat = 35
return abs(self.center.x - point.x) > buttonRadius || abs(point.y) > buttonRadius
}
func createPathCircle() -> CGPath {
let radius: CGFloat = 37.0
let path = UIBezierPath()
let centerWidth = self.frame.width / 2
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: (centerWidth - radius * 2), y: 0))
path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: radius, startAngle: CGFloat(180).degreesToRadians, endAngle: CGFloat(0).degreesToRadians, clockwise: false)
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
path.addLine(to: CGPoint(x: 0, y: self.frame.height))
path.close()
return path.cgPath
}
}
extension CGFloat {
var degreesToRadians: CGFloat { return self * .pi / 180 }
var radiansToDegrees: CGFloat { return self * 180 / .pi }
}
这就是我想要看到的(从https://betterprogramming.pub/draw-a-custom-ios-tabbar-shape-27d298a7f4fa)
而我得到的。
而UITabbarController
的Apple文档声明:
您不应试图操作存储在此属性中的UITabBar对象本身。
你可以找到很多很多自定义标签栏的例子。
对于您的特定方法,不要尝试重写var tabbar:
相反,如果您的TabBarController位于情节提要板中,则将其TabBar的自定义类分配给CustomizedTabbar
。
或者,如果您是从代码实例化控制器,您可以尝试以下操作:
override func viewDidLoad() {
super.viewDidLoad()
let tabBar = { () -> CustomizedTabBar in
let tabBar = CustomizedTabBar()
tabBar.delegate = self
return tabBar
}()
self.setValue(tabBar, forKey: "tabBar")
// ... the rest of your viewDidLoad()
}
不过,我建议阅读其他几个示例,并寻找一个通用的(可靠的)方法。
自定义UITabBar,包括可自定义tab bar的背景图、tab bar的高度以及每个tab的图片,满足各种界面需求。但是,没有文字。 [Code4App.com]
自定义中间有突起的UITabBar,demo中高仿了gogobot app底部不规则的TabBar。 [Code4App.com]
授人以鱼,不如授人以渔。Dism++的能力是有限的,无法覆盖的所有用户的需求。因此给程序添加了自定义规则功能,方便高级用户打造自己的专属工具。 创建一个空白的自定义规则文件 我们只需要在Config目录新建一个 Custom*.xml 这样形式的xml文件即可(*表示任何字符串,比如Custom1.xml或者Custom我爱我的家.xml,另外文件以UTF8或者Unicode编码保存)。初始文件内
任何关于这个或者如何使用drools api修改drools规则的想法都将是有用的。 注意:我不想为修改规则做字符串替换。
问题 你在写一段代码,最终需要创建一个新的类对象。你考虑将类的定义源代码以字符串的形式发布出去。 并且使用函数比如 exec() 来执行它,但是你想寻找一个更加优雅的解决方案。 解决方案 你可以使用函数 types.new_class() 来初始化新的类对象。 你需要做的只是提供类的名字、父类元组、关键字参数,以及一个用成员变量填充类字典的回调函数。例如: # stock.py # Example
我花了一天的时间将所有PMD和Checkstyle规则迁移到新的Squid规则,因为PMD/Checkstyle规则被标记为不推荐使用。 规则:BadConstantName_S00115_Check/S00115 我们所有的枚举都是用camelCase而不是CONSTANT_NAME实现的,例如: 比: 规则:MethodCyclomatic复杂性 迁移后,该规则报告所有equals和hashc