我们如何创建一个更大的中心UITabBar项目

Jay*_*iyk 41 custom-controls uibutton uitabbarcontroller uitabbar ios

我想知道我们如何创建一个更大的中心UITabBar,如下图所示?真的很漂亮!!!!

在此输入图像描述

小智 42

2017答案 - 只需Xcode

单击要显示的特定选项卡栏视图控制器中的选项卡栏按钮,

删除文本,只需将图像插入顶部设置为标签栏按钮的-25.

喜欢下图

在此输入图像描述

之后

转到资源,
选择您在标签栏按钮中
设置的图像,设置属性渲染为原始图像(如果您有彩色按钮,或者它将呈现为一种颜色)
如下所示, 在此输入图像描述

现在,你会得到你想要的, 在此输入图像描述

  • 这在视觉上效果很好,但它只接受选项卡栏范围内的水龙头。有什么方法可以记录圆的顶部1/3处的丝锥? (3认同)

Man*_*rig 24

我建议你看看下面的文章.它解释了如何自定义提升主按钮的标签栏.

码:

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
   button.center = self.tabBar.center;
else
{
 CGPoint center = self.tabBar.center;
 center.y = center.y - heightDifference/2.0;
 button.center = center;
}

[self.view addSubview:button];
Run Code Online (Sandbox Code Playgroud)

指南:https: //github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar

  • @cohenadair`self`将是`UIViewController`.我添加了一个与Github reposirtory的链接[example](https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar) (2认同)

jua*_*njo 7

斯威夫特3,4:

我在viewDidLoad我的子类中使用此代码UITabBarController:

let button = UIButton()
button.setImage(UIImage(named: "home"), for: .normal)
button.sizeToFit()
button.translatesAutoresizingMaskIntoConstraints = false

tabBar.addSubview(button)
tabBar.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
tabBar.topAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)

有时我也设置button.adjustsImageWhenHighlighted = false为模仿其他项的行为,或者更改约束constant属性以向上或向下移动按钮.


Mic*_*ann 5

这是移植的Swift 3版本@Kakashi的答案(以及+1给他们),我把它放在我的自定义UITabBarController子类中:

override func viewDidLoad() {
        if let newButtonImage = UIImage(named: "new__button") {
            self.addCenterButton(withImage: newButtonImage, highlightImage: newButtonImage)
        }
 }

 func handleTouchTabbarCenter(sender : UIButton)
 {
    if let count = self.tabBar.items?.count
    {
        let i = floor(Double(count / 2))
        self.selectedViewController = self.viewControllers?[Int(i)]
    }
 }

func addCenterButton(withImage buttonImage : UIImage, highlightImage: UIImage) {

        let paddingBottom : CGFloat = 10.0

        let button = UIButton(type: .custom)
        button.autoresizingMask = [.flexibleRightMargin, .flexibleTopMargin, .flexibleLeftMargin, .flexibleBottomMargin]
        button.frame = CGRect(x: 0.0, y: 0.0, width: buttonImage.size.width / 2.0, height: buttonImage.size.height / 2.0)
        button.setBackgroundImage(buttonImage, for: .normal)
        button.setBackgroundImage(highlightImage, for: .highlighted)

        let rectBoundTabbar = self.tabBar.bounds
        let xx = rectBoundTabbar.midX
        let yy = rectBoundTabbar.midY - paddingBottom
        button.center = CGPoint(x: xx, y: yy)

        self.tabBar.addSubview(button)
        self.tabBar.bringSubview(toFront: button)

        button.addTarget(self, action: #selector(handleTouchTabbarCenter), for: .touchUpInside)

        if let count = self.tabBar.items?.count
        {
            let i = floor(Double(count / 2))
            let item = self.tabBar.items![Int(i)]
            item.title = ""
        }
    }
Run Code Online (Sandbox Code Playgroud)