TableView selected background view switching orientations - iphone

I have a nib file that contains the view at 320 wide for portrait mode and it works fine.
cell.selectedBackgroundView = aView
I set the auto resize mask to allow the tableview to expand in landscape mode like so:
setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
but this does not expand the highlight view that remains at 320!
How do I get the highlight view to expand as well as the table view?
I have tried this:
[cell setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
but no good???
Thanks

selectedBackgroundView is a UIView too, meaning that it might help to aView.autoresizingMask = UIViewAutoresizingFlexibleWidth too.
Cheers!

Related

What is the exact size of the status bar at top of the iPhone screen?

I tried to add a UITableView programmatically to my view, but setting its parameters as follows doesn't work (the last cell is cropped).
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
I tried to replace the height by 460, which works pretty well. But I want to know the exact size of this bar :
Thanks for your help.
You should never hardcode the exact value of the height at any point in your code.
As long as you use default UI components (UINavigationController / UITableView / UICollectionView / etc.) you usually don't need to worry about the status bar height at all. These ViewControllers should layout correctly on any device and any orientation.
If you do have custom layout needs, you should refer to the safeAreaLayoutGuide on UIView, instead of hardcoding a height:
https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide?language=objc
But to make this answer complete - the size of the status bar is different on different devices and different orientations:
Most devices up to the iPhone X have a 20pt height in portrait & landscape.
(20px, 40px, 60px in #1x, #2x, #3x)
On iPhone X in portrait it's 44pt (so 44px, 88px, 132px accordingly).
In landscape the height is different though.
Your parent view controller will resize it's view to the right size. You can
make your view controller load a subclass of UIView and override -layoutSubviews
insert your subview with the proper starting size ([[ MyViewClass alloc ] initWithFrame:superview.bounds]) and the proper autoresizing mask. It's important when using autoresizing struts & springs that you give your view the proper size to start with.
BTW--another problem with hard coding the status bar height: it's sometimes double-height. (when the user is recording audio, making a phone call, using internet tethering, using navigation, etc.)
This answer is to Rob's question in the comments to the original question:
So if I don't hard-code my screen size, how can I set it automatically to fit my UIViewController?
Try this:
- (void)viewDidLoad {
[super viewDidLoad];
table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
// all other table setup
table.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:table];
}
This assumes you want the table view to fill the view controller's view. Adjust as needed.
This will ensure the table view's size changes as the view controller's view size changes. This covers rotations, in-call status bars, etc.
You should't use set pixel dimensions to size something to the screen. use you view's frame. i.e.
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
Anyway. It's 20 points. 20px non retina. 40px retina.
Update for iPhone X
Previously all iPhone devices have a status bar height of 20pt. But on iPhone X, it's 44pt. So be careful when implementing your UI.

UITabBarController hides but white space remaining

I have UITabBarController in my app.
In a particular view I want to hide the tabbar and display the content up to the below line.
For that I used the code in the view
-(void)viewWillappear{
[self.tabBarController.tabBar setHidden:YES];
}
It depends what your content is, but if it's inside a container, like a UIView, you can change the autoResizingMask to be
myView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
and it should do the right thing.

Layering UIViews so that bottom most layer can be seen

I have a UIView to which i add a background image. Then to that view i add a scroll view. To the scroll view i add some UIButtons.
I would like to be able to set the scroll view to be transparent (still being able to see the UIButtons) so that i can see the background image underneath it, so that it shows between the buttons.
I have tried setting the scrollview background to [UIColor clearColor] but this doesnt work.
Thanks.
In addition to setting the backgroundColor to [UIColor clearColor], you also have to set
scrollView.opaque = NO;
Change the UIScrollView.alpha level to 0.0f

Auto resize subviews on device rotation

I have all my subviews set up so that they are based on self.view.
EG: UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,(self.view.frame.size.width-20),(self.view.frame.size.height-90))];
however when the view rotates (shouldRotateToDeviceOrientation or whatever) the views all stay the same size. How can I make them change shape to fit? Can I do this automatically?
Thanks
Absolutely. Take a look at the autoresizingMask property. If you set your image view, in this case, to have an autoresizing mask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight, then when its parent view resizes (as it may or may not automatically do when your app rotates—you might have to set a similar autoresizingMask on the parent view), it'll maintain the exterior margins you set up for it.
Just in case you have a view in the bottom of parent view this should help:
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
Where self is your child view, that should be resized. If you do not set UIViewAutoresizingFlexibleTopMargin, then in horizontal mode you will not see your view, if it was in the bottom in vertical mode.
Also do not forget to set:
self.autoresizesSubviews = YES;
And autoresizing modes for all child view in your view. So that when it is resized, everything inside it also gets resized.
you have to [view setAutoresizingMask:...]

UIView subclass won't autoresize

I've been looking for background information on resizing, but I haven't been able to find much. I know I need to set autoresizesSubviews on the superview and autoresizingMask on the subview.
I have done this, and my UIImageViews properly resize, but my custom UIView subclass does not.
The code for the UIImageView:
UIImageView *newX = [[[UIImageView alloc] initWithImage:dot] autorelease];
[newX setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[newX setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 1)];
[self.view addSubview:newX];
The code for the custom UIView subclass:
trace = [[TraceView alloc] initWithFrame:self.view.bounds];
[trace setAutoresizingMask:(UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin)];
[self.view addSubview:trace];
From what I can tell, the view rotates as expected, but does not resize. I can no longer see the bottom of it, and it doesn't fill the screen to the right.
Do I have to add anything to the UIView subclass to make resizing work properly?
EDIT:
I've changed the autoresizingMake to (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) and called [subview setNeedsDisplay]; I'm not sure why this is necessary as the UIImageViews work fine without it, but it now behaves as expected.
I think you're missing UIViewAutoresizingFlexibleHeight or width on the subview. Otherwise I think the view just moves around according to the margins but doesn't resize.
You mention rotation. Is the superview that contains your custom view actually resizing correctly? If in doubt, set its background to some odd color. If it did not resize correctly after the orientation change then you should see a white or grey bar on the right.