Rounded corners for UITabBar

吃可爱长大的小学妹 提交于 2019-12-06 10:42:57

问题


I would like to create a UITabBar with rounded corners. Is there a way I can make the UITabBar have rounded corners? It would take the shape of the second picture.

The app starts off with a tableView. When the user taps a topic, they are sent to a tabBar controller.

-----edit-----

This is my AppDelegate:

func application(_application: UIApplication,
willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool{

    let tabBarController = window?.rootViewController as! UITabBarController
    let image = UIImage(named: "bar")
    let tabBarImage = resize(image: image!, newWidth: tabBarController.view.frame.width)
    tabBarController.tabBar.backgroundImage = tabBarImage


    return true
}

func resize(image: UIImage, newWidth: CGFloat) -> UIImage {

UIGraphicsBeginImageContext(CGSize(width: newWidth, height: image.size.height))
image.drawInRect( CGRect(x: 0, y: 0, width: newWidth, height: image.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}


回答1:


Hey I used a simple property of the tabBar which is backgroundImage.

So, I added in the appDelegate in didFinisLaunchingWithOptions:

let tabBarController = window?.rootViewController as! UITabBarController
    let image = UIImage(named: "bar")
    let tabBarImage = resize(image: image!, newWidth: tabBarController.view.frame.width)
    tabBarController.tabBar.backgroundImage = tabBarImage

and my custom method to change the size of the image:

func resize(image: UIImage, newWidth: CGFloat) -> UIImage {

    UIGraphicsBeginImageContext(CGSize(width: newWidth, height: image.size.height))
    image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: image.size.height)) // image.drawInRect( CGRect(x: 0, y: 0, width: newWidth, height: image.size.height))  in swift 2
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

I used a png image as the background, the same that you posted but with clear color instead of the black color.

Hope this helps




回答2:


Hope this help you

self.tabBar.layer.masksToBounds = true 
self.tabBar.isTranslucent = true 
self.tabBar.barStyle = .blackOpaque 
self.tabBar.layer.cornerRadius = 20 
self.tabBar.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]



回答3:


override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: view.frame.width, height: tabBar.frame.height), cornerRadius: 15)
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    tabBar.layer.mask = mask

}



回答4:


self.tabBarController?.tabBar.layer.cornerRadius = 20
self.tabBarController?.tabBar.layer.masksToBounds = true


来源:https://stackoverflow.com/questions/38127839/rounded-corners-for-uitabbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!