效果圖

A4D71D47-1C38-4409-A785-E44C3BED860B.png
代碼實現(xiàn)
代碼的核心就是計算按鈕的位置
let cout = CGFloat(childViewControllers.count)
let width = tabBar.bounds.width/cout
composeButton.frame=tabBar.bounds.insetBy(dx: 2*width, dy:-20)
composeButton.layer.cornerRadius=width/5
composeButton.clipsToBounds=true
tabBar.addSubview(composeButton)
這樣會導致點擊按鈕和消息之間出現(xiàn)空白,原因就是容錯點的出現(xiàn)。
容錯點:兩個按鈕之間會有間隙,用手點擊不會出錯,但是鼠標比較精確。
解決方法:把寬度減1,// 將向內縮進的寬度減少,能夠讓按鈕的寬度變大
let cout = CGFloat(childViewControllers.count)
let width = tabBar.bounds.width/cout-1
composeButton.frame=tabBar.bounds.insetBy(dx: 2*width, dy:-20)
composeButton.layer.cornerRadius=width/5
composeButton.clipsToBounds=true
tabBar.addSubview(composeButton)
這里多說一句,tabBar.bounds.insetBy(dx: 2*width, dy:-20)這里類似與OC的cgrectInsert,正數(shù)向內縮進,負數(shù)向外擴展。