iOS8 How to set TabBarItem images - ios8

It seems something has changed with iOS8 and now none of my tab bar icons are showing up properly. Most of the time they don't show until the tab is active:
But sometimes they don't show up at all and give me just a big blue box (like whenever I dismiss a view that covered the whole window):
This is what I did pre iOS8:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"paintbrush-black.png"]];
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
tabBarItem1.title = #"";

as mentioned, if you take a look at:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITabBarItem_Class/index.html#//apple_ref/occ/instm/UITabBarItem/setFinishedSelectedImage:withFinishedUnselectedImage:
you will notice that this method is deprecated, try to change:
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"paintbrush-white.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"paintbrush-black.png"]];
to:
[tabBarItem1 setImage:[[UIImage imageNamed:#"paintbrush-white.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"paintbrush-black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
You may also have problems with the image size, depends of the size of image when testing in iPhone 5 screen and iPhone 6 screen for #2x images

Did you try setSelectedImage:?
UIImage *image = [UIImage imageNamed:#"img.png"]
[tabItem setSelectedImage:image];
It works on my part.

This method is deprecated in iOS 8:
Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal

Related

iOS 13 - UIPopoverPresentationController sourceview content visible in the arrow

When I am displaying some view in UIPopoverPresentationController and presenting it as popover
popoverCon?.modalPresentationStyle = UIModalPresentationStyle.popover
the content have moved upward toward and a some part is being display in the arrow.
Further I had border around the popover
popoverCon?.view.layer.borderColor = .orange
popoverCon?.view.layer.borderWidth = 1.0;
popoverCon?.view.layer.cornerRadius = 10.0;
popoverCon?.view.layer.masksToBounds = false;
it is not showing toward the part where arrow is but it displays a little of the border line in the tip of the arrow.
This was working fine until iOS 12 but in iOS 13 this issue is coming.
Any suggestions on how I can solve this?
The top of my tableView content was cut off by the arrow. This is how I fixed it in my case (code inserted in my tableViewController Swift file):
override func viewSafeAreaInsetsDidChange() {
if #available(iOS 11.0, *) {
super.viewSafeAreaInsetsDidChange()
self.tableView.contentInset = UIEdgeInsets(top: self.tableView.safeAreaInsets.top, left: 0, bottom: 0, right: 0)
}
}
My solution in Obj-C, for those who need an obj-c solution.
I had previously only popovercontroller, that was creating the error like shown in the question. I renamed it to childController for clarity and created a containing popoverController to make the solution given by #SaintMSent work in my situation of only one view originally. Also used https://stackoverflow.com/a/47076040/2148757 solution and https://useyourloaf.com/blog/self-sizing-child-views/ to resize appropriately since all of my childControllers set the preferred content size frequently.
//Create container popover controller and add child to it
UIViewController* popoverController = [[MyParentPopoverController alloc] init];
[popoverController.view addSubview:childController.view];
[popoverController addChildViewController:childController];
[popoverController setPreferredContentSize:childController.preferredContentSize];
//set popover settings on container
popoverController.modalPresentationStyle = UIModalPresentationPopover;
popoverController.popoverPresentationController.sourceRect = sourceRect;
popoverController.popoverPresentationController.sourceView = buttonView;
popoverController.popoverPresentationController.permittedArrowDirections = direction;
//Fix ios13 'bug' that Apple claims is a feature
UILayoutGuide* guide = popoverController.view.safeAreaLayoutGuide;
childController.view.translatesAutoresizingMaskIntoConstraints = NO;
[childController.view.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
[childController.view.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
[childController.view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
[childController.view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
[popoverController.view layoutIfNeeded];
//Show the popover
...
#interface MyParentPopoverController : UIViewController
#end
#implementation MyParentPopoverController
-(void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container {
[super preferredContentSizeDidChangeForChildContentContainer:container];
[self setPreferredContentSize:container.preferredContentSize];
}
#end
Note: I didn't check for ios11 compatibility because my user base is restricted to not use it.
It is definitely a feature, they want you to use safe area since iOS 11, actually, but it seems now they want to force you to use it
Had the same problem as you, this worked for me
https://useyourloaf.com/blog/safe-area-layout-guide/
Definitely a bug. When you have a situation where you use UIPopoverArrowDirectionAny you will see that the problem only exists when the arrow is at the top or left of the popover and not when the arrow appears at the right or the bottom of the popover. If you make adjustments in your code to compensate it will work if you use UIPopoverArrowDirectionUp or UIPopoverArrowDirectionLeft but will not display correctly using that adjustment when using UIPopoverArrowDirectionAny and the popup appears above or to the right of the target rectangle.
I don't have an 'answer' yet, but I have identified what's going on and why it's so hard to fix.
ios13 UIPopoverViewController showing UITableViewController - Safe Area problems / Missing parts of table
Basically, any UITableView that has headers or footers is going to be broken in iOS 13 unless there's some way to alter the _UITableViewHeaderFooterViewBackground
That is notoriously problematic and doesn't play nicely with Auto-Layout - it's been known about for years, but Apple have never fixed it or made it easier to deal with and more publicly known.
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=video&cd=1&cad=rja&uact=8&ved=0ahUKEwibouuozfvkAhVCXRUIHVGsBegQtwIIKjAA&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DajsCY8SjJ1Y&usg=AOvVaw3_U_jy9EWH2dJrM8p-XhDQ
https://spin.atomicobject.com/2016/10/12/auto-layout-uitableview/
I'm unable to push my app to the App Store until I get this sorted.. I hope someone can identify how to manipulate this view so that it stops pushing the boundaries of the table out of whack with AutoLayout which causes this safe area intrusion.
Searching on the internet I got help from following link
Twitter
so I had to add safe area and manage my views accordingly
CGFloat topPadding = 0.0;
if (#available(iOS 11.0, *)) {
topPadding = self.view.safeAreaLayoutGuide.layoutFrame.origin.y;
}
Swift:
var topPadding: CGFloat = 0.0
if #available(iOS 11.0, *) {
topPadding = self.view.safeAreaLayoutGuide.layoutFrame.origin.y
}
but I haven't got solution to the border problem of mine yet.
Edit:
Temporarily I did solved the border problem by creating an invisible view on popover and giving it same frame as safe area and drawing its border.
You should use constraints. And also pay attention to topAnchor. It must be safeAreaLayoutGuide.topAnchor. In my case, it works correctly. For example:
[NSLayoutConstraint activateConstraints:#[
[toolbar.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
[toolbar.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
[toolbar.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[toolbar.heightAnchor constraintEqualToConstant:50]
]];
Embed the contents of the popover in another view with "safe area relative margins" on. This should have -21,-21 as the origin. Turn off vertical and horizontal auto resizing. Seems to work, although you lose auto-stretching.
Setup your popover's content UIViewController like such:
NSLayoutConstraint.activate([
myContentView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
myContentView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
myContentView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
myContentView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
])

imageWithSize Flakey with MPMediaItemArtwork in iOS8

In my app, I display album artwork on a button. On an iPhone 5, the button bounds size is 273x269.5. My code to size the artwork has always been very straightforward and nothing has changed.
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
MPMediaItemArtwork *iTunesArtwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
CGSize buttonBounds = self.albumArtworkButton.bounds.size;
UIImage *resizedImage = [iTunesArtwork imageWithSize: CGSizeMake (buttonBounds.width, buttonBounds.height)];
Suddenly with iOS8, the execution of this results in resizedImage = nil. What is really strange is that if I change the last line, the execution of the following line:
UIImage *resizedImage = [iTunesArtwork imageWithSize: CGSizeMake (300, 300)];
results in a valid image (i.e. resizedImage is not nil and the image can be displayed).
Any ideas what might cause this? I haven't changed anything, but the code broke.
I'm seeing this new bug in iOS8 with my apps also. It was only happening for some of the coverart and it depended on what my target size was. it seems the bug has something to do with it can't reduce an image by some factor. For example I have some images from iTunes that are 600x600 and when I try to get an imageWithSize of 100x100 it returns nil while trying to get an imageWithSize of 200x200 works. I have one image that is 200x203 and 100x100 doesn't work, 101x101 doesn't work, but 102x102 will work - I thought maybe the highest resolution you wanted needed to be more than 1/2 of the original dimension to avoid the bug but that didn't turn out to be true based on the 600x600 images working at 200x200. perhaps its the total memory size change or something like that - anyway it's hard to figure out why a bug happens when you don't have their source so I stopped guessing and used this workaround. basically just use the original bounds of the MPMediaItemArtwork object when you request the UIImage via imageWithSize. Then make sure the UIImageView you use with the UIImage has it's contentMode set to display the image properly (UIViewContentModeScaleAspectFill, UIViewContentModeScaleAspectFit, UIViewContentModeScaleToFill).
// display album artwork
MPMediaItemArtwork* artwork = [representativeItem valueForProperty:MPMediaItemPropertyArtwork];
CGRect bounds = [artwork bounds];
UIImage* artworkImage = [artwork imageWithSize:bounds.size];
if (artworkImage)
{
[cell.imageView setImage:artworkImage];
}
else
{
[cell.imageView setImage:[UIImage imageNamed:#"AlbumDefault.png"]];
}

Xcode 5, why isn't the image resizing?

Hello I am trying to resize a UIImage, but even though I'm not getting any errors it is not working.
hers the code of .h file
IBOutlet UIImageView *Fish;
heres the code of .m file
Fish.frame = CGRectMake(0, 0, 300, 293);
What am I doing wrong?
Thanks for any help
The image is probably not resizing because you are just resizing the image view. Make sure in your storyboard that you make the image view (Fish), have the move ScaleToFill. I can't do screenshot due to reputation ( sorry :( )
Alternately, if your goal is not to resize the image view but to resize the image it is holding, you can do this:
UIImage *image = Fish.image;
UIImage *image = YourImageView.image;
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(80,60);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;
[image drawInRect:thumbnailRect];
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
YourImageView.image = tempImage;
and you would set thumbnailRect to whatever size you want.
Hope this helps! Please search Nerdy Lime on the app store to find all of my apps! Thanks!
I bet your outlet is not hooked up. In your "viewDidLoad" method, try doing this:
if(Fish)
{
Fish.frame = CGRectMake(0, 0, 300, 293);
} else {
NSLog(#"Fish is null; why did I forget to connect the outlet in my storyboard or xib?");
}
And this isn't the best way to resize your UIImageView. If you're using regular springs & struts, you can grow an outlet by clicking the springs & struts to grow based on the superview's size, e.g.:
And if you're doing AutoLayout, there's a different thing you can do (basically pin your view to all four sides of the superview).
Here is how I do it:
1) select the outlet / object you want to add constraints to (in your case, it'll be the fish image view)
2) see the segmented control at the bottom of the Interface Builder window? Click on the second one and you'll see a popover view open up with a list of possible constraints to add.
3) In my example, I'm adding constraints in my ImageView to always be 10 pixels from each edge of the superview (note the four "10"s and solid red lines meaning I'm adding four constraints).
AutoLayout is a pain to get accustomed to (and I'm still learning it myself), but I suspect that once one gets the hang of it, it'll be a powerful new tool especially as Apple brings in additional iOS screen sizes in the very near future.

iOS Colorwithpattern - using a custom iPhone5 image

For my app's loading screen and splash screen, I used up with two different methods to display my iPhone 3, iPhone 4 and iPhone 5 images appropriately.
For the loading screen, simply adding -568h#2x to your image is enough to support iPhone5.
For the splashscreen, I used a series of (if height == ) cases to check the height of the UIScreen's bounds and sourced the appropriate image to the image view. It was apparent to me here that the -568h isn't universally recognized as an iPhone 5 image. It is only applicable to the loading screen.
Now, in my AppDelegate, I'm setting a the background image. All my subviews have a transparent background, so they are supposed to show through to the background image. However, I am having the most trouble setting up the background image here.
Simply passing in the "Background.png" Adding the -568h#2x to the end of the filename does not work. It will do the non-retina and the 3.5'' retina display, but will not pick up for the 4'' display.
ie:
self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
If I use the above snippet of code, the iPhone4 picture is used instead of the iPhone5 picture and this is not what I want.
I moved onto trying to do the same thing as I did with the splashscreen. I had a bunch of if cases:
CGFloat height = [UIScreen mainScreen].currentMode.size.height;
if ( height == 1136 )
{
//Choose image here
}
else if (height == 960)
{
//Choose image here
}
else if (height == 480)
{
//Choose image here
}
self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:chosenImage]];
But the colorWithPatternImage function thinks that the chosenImage goes by 1 point = 1 pixel. So I end up with a non-retina picture attempting to fit onto the iPhone 5 screen. What exaclty does it look like? Looks like only the top left quadrant of the image I wanted is displayed over the entire iPhone 5 screen. It looks like no retina scaling was applied.
I need the iPhone to recognize that I have a retina-friendly iPhone 5 picture to be used as the background. How do I do this?
I found this code here, perhaps it helps you:
UIImage *tmpImage = [UIImage imageNamed:#"background-568h#2x.png"];
UIImage *backgroundImage = [UIImage imageWithCGImage:[tmpImage CGImage]
scale:2.0
orientation:UIImageOrientationUp];
self.window.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];

UITabBar setSelectionIndicatorImage in iOS 4

i want know how i can use an image to the selected item in a UITabBar, in ios5 i'll do in this way:
[[myTabBar tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"TabBar_Activated.png"]];
but in iOS 4 that line give me an error, how i can do?
Try with this..
This will show the image when you select the tab...
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selection-tab.png"]];
And this will set the image for tab bar...
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Me" image:[UIImage imageNamed:#"tab-me.png"] tag:1];
//Set this tabitem in current view's tabBar.
[self setTabBarItem:tab1];

Resources