Prevent UITableViewCell.backgroundView From Resizing Image On Orientation Change - ios

I have a table cell with a custom backgroundView which looks great while in portrait, however when I move to landscape the backgroundView gets stretched to match the landscape width. How can I make it so the backgroundView stays the same height/aspect ratio?
This seems to do nothing:
self.backgroundView.autoresizingMask = UIViewAutoresizingNone;
Is there a solution to this? Here's some screenshots to illustrate. The first image shows portrait view properly, the second shows landscape view with the middle rows scaled when I want them to be the same size as they were in portrait, just located in the middle (much like the images above them, which are actually just UIImagViews added directly to the cell, rather than changing the backgroundView property).
Thanks!

Figured this out. All I needed to do was set the contentMode.
backgroundView.contentMode = UIViewContentModeCenter;

Related

UIImageView will not resize to match UIView's frame

I want my background to be an image, so in IB I set a UIImageView with the constraints to be the same size as the its UIView. However, when I select "back" on the UINavigationBar, when the NavControl pops to the previous VC, this is what's there, briefly, before it disappears:
That is, the UIImageView is so big that it is almost double the width of the UIView, so as it slides off-screen to the right, the UIImageView's image drags on before finally disappearing.
I have also tried to do this programmatically, but even if I set the UIImageView's frame to be CGRectMake(0, 0, 100, 100), it still goes off to the left side of the UIView, as if the UIView's origin weren't at the top left corner, but slightly off the screen. Is this possible? How do I make it so the image fits the screen and doesn't go off?
If it's at all relevant, the jpg I'm using is in Images.xcassets and, although I can select it in IB and it appears, the UIImageView's image is white/blank, so in my viewDidLoad method I need to specify self.imageView.image = [UIImage imageNamed:#"hockeyIce.jpg"].
go to Main.storyboard
select the view in question
click Attributes Inspector
scroll down to View, select Mode: Scale To Fill
OK, the solution was to add [self.imageView setClipsToBounds:YES] to my viewDidLoad. It turns out that this is a common problem with aspectFill.

TableView Rotation resizes subviews

My app supports both portrait and landscape mode. I have a TableViewController that has some issues when i rotate from portrait mode to landscape mode. I want subviews of TableView not to change neither orientation nor size. But i cannot find the way. I tried from storyboard to uncheck Autoresize subviews but it did not work. My TableView is into a TabBarController. It is strange, that in another ViewController i have a similar TableView with exactly the same attributes that works fine. So i added some pics with the problem and with what i want.
The only code that handles TableView orientation changes is in viewDidLoadMethod the below:
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
The first image is from Portrait mode
The second from troublesome Landscape mode
And the third from the right TableViewController and the result that i want
Any advice would be helpful.
What you can do is setting a constraint on your imageView and force the width and height to stay fixed.
The issue was in size inspector. I had to keep autoresizing of ImageView only TopLeft.

UIView inserted into UIWebView's scrollview, is not clickable in landscape mode

so basically, for reasons in this answer ive put a UIView inside a webview so that it acts as a header to the view. its works perfectly for ipad, but on iphone when the webview is rotated landscape, the header UIView isnt clickable.
ive tried in the - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation to explicitly set the userInteraction = true of the header view but this didnt work.
i tried removing it from the superview and adding it back, but to no avail.
i have some debug code that lets me see the class of the view im clicking on, and when i click on the header view i can see its clicking on the scrollview behind it (which is part of the webview). so it just seems to be ignoring the view completely (even though it works in portrait mode, and works in landscape and portrait on ipad).
does anyone know why the click events are going straight though the uiview?
update:
something interesting ive found is that if the webview doesnt resize when the view rotates, then the header is still clickable, so it must be something to do with the resizing of the webview screwing things up
also, the background from the scrollview is like coming through the header view it seems (header = grey, scrollview = yellow) pics here. when rotated back to portrait the grey shows up again and is clickable
Give the scroll view a background color to see where it is. If the button is outside of the scroll view it is still visible (if clipsToBounds is NO, which it is by default) but not clickable.
Find out where the superview of the button is and make the resizing in a way that it is big enough after resizing.
strangest solution, but instead of having the header view autoresize in all directions, i set it like so
something strange must have been happening and making its frame set wrong somehow

UIBarButtonItem support portrait and landscape sizes with a custom view?

I have a UIBarButtonItem which is just an image, I wanted to use this image as the background image of a bar item with an empty string title.
But the width didn't work out correct, in order to have the correct dimensions, I made a UIBarButtonItem with a custom view which was a UIButton with the image set.
The problem I have is that in landscape mode this custom view doesn't resize to fit correctly the smaller navigation bar.
I tried making my button autoresize by allowing flexible height, but it's preserving the top and bottom margins of the portrait and now my button is very squashed.
The reason I originally wanted to use a bar item with empty title was to use the appearance protocol to set the background image for bar metrics default and landscape to bypass this problem.
How can I make my UIBarButtonItem support portrait and landscape sizes with a custom view?
I have a dedicated landscape image so that it's just smaller, not distorted which I want to use.
Portrait:
Landscape:
Landscape with autoresizing, note the margins from portrait are causing an exaggerated squashing of the bar button.
The best answer is to provide two images one for portrait mode and the other is for the landscape mode and your code will be like
UIBarButtonItem *barBtnLeft= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourImg.png"] landscapeImagePhone:[UIImage imageNamed:#"yourImg_Landscape.png"]style:UIBarButtonItemStyleBordered target:self action:#selector(yourSelector:)];
I know this post is old, but it luck the right answer, I hope newcomer, like me, will get the help with this solution.
I should mention here that landscape image should be smaller by 0.75f scale.
Is the image you're using resizable? Did you create it with - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets?
If you just want it to maintain it's aspect ratio, and the custom view you've used is a UIImageView (which I assume it is), you can set the contentMode property of the image view to make it scale while maintaining aspect ratio. You're probably gonna want to use UIViewContentModeScaleAspectFit.

Background color (colorWithPatternImage) not filling entire view for iPad

I am trying to set the background color with a checkered image I have. I am currently using this in my iPhone app and now am trying to create a universal app. When I use this:
UIImage *bgimg = [UIImage imageNamed:#"square_bg.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:bgimg];
It works fine for my iPhone app. When it loads on the iPad in portrait mode, there is about 20px worth of white space at the bottom. When it loads in landscape, it is fine. If I rotate from portrait to landscape, then back again, it is also fine.
What am I missing that will fill the pattern all the way to the bottom of my view?
Update
If I set the color to a solid color, [UIColor blueColor], the entire screen fills in correctly. So why the discrepancy from colorWithPatternImage and a solid color?
Update 2
The file I am using is a PNG file with dimensions of 252x230. I've used this image to make patterns in the iPhone version of this app with no issues. I'm not sure if the file size or dimensions have any bearing on this issue, but I thought I would put it out there just in case.
Perhaps the view is not set to the correct height. If you change backgroundColor to a solid color will it fill the entire screen?
20px sounds like the status bar height. Could you tell us the view frame?
Is your autoresizing set correctly to dynamic width & height and fixed borders?

Resources