Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iPhone X selectionIndicatorImage #191

Open
SwiftRabbit opened this issue Sep 13, 2017 · 15 comments
Open

iPhone X selectionIndicatorImage #191

SwiftRabbit opened this issue Sep 13, 2017 · 15 comments

Comments

@SwiftRabbit
Copy link

SwiftRabbit commented Sep 13, 2017

Having an issue trying to set the selectionIndicator Image.

self.tabBar.selectionIndicatorImage = UIImage.imageWithColor(UIColor.black, size: CGSize(width: tabBar.frame.width/CGFloat((tabBar.items?.count)!), height: tabBar.frame.height))

I create an image with a black background and this is what I get:

capture d ecran 2017-09-13 a 11 26 35

@albertml
Copy link

did you find a solution for this?

@SwiftRabbit
Copy link
Author

No, still pending :( (Found another bug making a vertical transition though)

@albertml
Copy link

albertml commented Sep 28, 2017

Maybe it's a bug in iphone X. I will inform you when I've found a solution.

@casperzandbergenyaacomm
Copy link

casperzandbergenyaacomm commented Sep 28, 2017

I have the same problem but I got a bit closer using:

.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 0, bottom: 0, right: 0))

This makes the positioning correct but adds padding around it.. :/

Edit:

Fixed by making the image 1 pixel less in height.
https://stackoverflow.com/questions/46276728/uitabbar-selectionindicatorimage-height-on-iphone-x

@premanandchandrasekar
Copy link

@casperzandbergenyaacomm solution works for me.

@tacovollmer
Copy link

tacovollmer commented Mar 10, 2018

It appears capsInsets top or bottom should not be 0 for the positioning to be correct on iPhoneX. So using CGFloat.leastNonzeroMagnitude for top or bottom also works. e.g.:

.resizableImage(withCapInsets: UIEdgeInsets(top: .leastNonzeroMagnitude, left: 0, bottom: 0, right: 0))

@SureshChary
Copy link

SureshChary commented Mar 27, 2018

@casperzandbergenyaacomm and @tacovollmer I followed ur steps. but still getting some gap in all iPhone versions. Please suggest how to fill the gaps.
My Code: self.tabBar.selectionIndicatorImage = makeImageWithColorAndSize(color: UIColor.white, size: CGSize(width: tabWidth, height: tabHeight-1)).resizableImage(withCapInsets: .zero, resizingMode: .stretch)

screen shot 2018-03-27 at 16 53 53

@tacovollmer
Copy link

I haven't been able to fill to the bottom @SureshChary. I'm interested to know if anyone did manage.

@casperzandbergenyaacomm

@SureshChary I have the same as you and it works for me, are you sure the tabHeight is tabBar.frame.height?

My code for the bar in the image.

let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width/numberOfItems, height: tabBar.frame.height)
let color = tabSelectedColor
        
tabBar.selectionIndicatorImage = UIImage
    .imageWithColor(color: color, size: tabBarItemSize)
    .resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
extension UIImage {
    
    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect: CGRect = CGRect(x: 0.0, y: 0.0, width: Double(size.width), height: Double(size.height))
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

image

Note: I do not use this library

@SureshChary
Copy link

Yes, let tabHeight = tabBar.frame.height

I tried your above code. But again it is failing in iPhone X 11.2. any idea?
Here is code:
let tabWidth = (tabBar.frame.width/CGFloat(tabBar.items!.count)) let tabHeight = tabBar.frame.height self.tabBar.selectionIndicatorImage = imageWithColor(color: UIColor.white, size: CGSize(width: tabWidth, height: tabHeight)).resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))

screen shot 2018-03-28 at 12 01 20

@casperzandbergenyaacomm

@SureshChary You need to call that code in the viewDidLayoutSubviews (after the height sizing for iPhone X)

@SureshChary
Copy link

Perfect!! Worked and thanks @casperzandbergenyaacomm

I feel iPhone X Tab UI is ugly bcoz of height-83. is it Ok for Apple?

screen shot 2018-03-28 at 15 08 40

@KoukuntlaAditya
Copy link

I faced the same issue and was able to fix it by adding an extra rect in the imageWithColor extension,

screen shot 2018-10-16 at 7 52 16 pm

This is how it looked after the fix.
screen shot 2018-10-16 at 7 53 10 pm

@casperzandbergenyaacomm

I would actually advise against using a background color on your tab bar buttons nowadays, they look off on the current generation of iPhones. A selected icon and unselected icon is a better way to show wether the tab is active.

@mushthak
Copy link

https://stackoverflow.com/a/46310166/3629513
This worked for me by just reducing the height value by 1.
Ya that's odd

let numberOfItems = CGFloat(tabBar.items?.count ?? 0)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height - 1)
self.tabBarController?.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.blue, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants