How can I adapt an UITableView height to iPhone 4-inch screen? I have the following configuration, which works for iPhone 4. However on iPhone 5 (see image below) there is a blank space because the tableview does not rezise. The tableview is scrollable.
I've autolaout enabled and I tried defining horizontal and vertical spacing constraints for all views and the tableview, but I get the same results (see image below). Any suggestion?
This viewcontroller is placed inside a placeholder since I created a custom menu. I tried settting the constant of the placeholder in the parent view with the following code. Also not working, the tableview is clipped.
-(void) viewDidLoad{
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
NSLog(#"iphone5");
self.dynamicTVHeight.constant = 465;
[self.placeholder needsUpdateConstraints];
}
else{
NSLog(#"iphone4");
self.dynamicTVHeight.constant = 375;
[self.placeholder needsUpdateConstraints];
}
And these are the constraints of my parentview, which implements a custom navigation menu. I did two tests:
With the vertical and horizontal spacing constraints:
And with the height constraint in my placeholder:
You need to set the bottomSpaceToContainer (or Bottom layout) to 0.
Make sure that there are no warnings or errors in your nib file (indicated by the yellow or red sign on your UITableView). If there are none, make sure you have all 4 constraints needed to fully describe your UITableView's position.
What you want :
To compare iphone 4 and iphone 5 :
Hope that will help =)
Related
I am trying to create Universal application using iOS storyboard within Xcode 7 and iOS 9. In the storyboard concept I can see lots of methods (autolayout, auto constraints, adaptive UI, etc.). I am confused by those methods of design. Please give me a solution for image view show on center for all iPhone and iPad.
Its Quite easy ..
1.place the uiimageview to center
Then add the Below constraints
1.centre horizontal to the view && centre vertical to the view
2.set it to aspect ratio
See the image given below.
centre horizontal ,centre vertical ,aspect ratio set this three you will get ...
You can try size classes work with many different screen sizes.
Change constraint like this or other
If you want to design Universal application in iOS then you must add constraints base on the percentage(%) and There are few situations or cases for which you can not added the constraints from the Starboard but don't wary about that we can do that programatically.
Have you ever try KVConstraintExtensionsMaster library to apply constraints that I have implemented. This library having the method that will update , apply , access and remove the constrains those are applied either applied by Programmatically or from Interface Builder(StoryBoard/xib).
Programmatically imageView show on center for all iPhone and iPad.
- (void)viewDidLoad
{
[super viewDidLoad];
/* preparing View Hierarchy */
UIImageView *imageView = [UIImageView prepareNewViewForAutoLayout];
[self.view addSubview:imageView];
[imageView setBackgroundColor:[UIColor blueColor]];
/* applying constraints */
[imageView applyConstraintForCenterInSuperview];
/* Change the Ratio 0.0 to 1.0 to vary the height and width */
[imageView applyEqualHeightRatioPinConstrainToSuperview:0.8];
[imageView applyEqualWidthRatioPinConstrainToSuperview:0.8];
/* Comment the above both RatioPinConstrainToSuperview methods and uncomment below methods and run on both iPhone or iPad and see the magic.*/
/*
[imageView applyWidthConstraint:280]; // fixed width for iphone
[imageView applyHeightConstrain:280]; // fixed height for iphone
// update width constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];
// update height constraint with proper iPad ratio
[imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
*/
}
From StoryBoard imageView show on center for all iPhone and iPad.
Step 1. Drag and place imageView in view of ViewController.
Step 2. Now Add constraint Horizontal Center in Container and Vertical center in Container.
Step 3. Add constraint Equal Width and Equal Height.
Step 4. Now update multiplier of Equal Width constraint and Equal Height.
My problem is very similar to UIToolbar not displaying on iPhone 4 and iPhone 4s
I am using Xcode 7 and am trying to get a 'legacy' UINavigationController based iPhone app up and running on various iPhone screen sizes. By legacy , I mean it does not use Storyboards etc. The views are loaded from an .xib.
The app is a classic UINavigationController app with UITableViewControllers but with a UIToolBar at the bottom underneath the table view. The TableView and ToolBar are subviews of the view of the ViewController.
Works great on iPhone5/5s/6/6s.
But on the iPhone 4/4s the toolbar is off the screen. Oddly if I rotate the screen to landscape, the toolbar appears. Rotate back, it vanishes. I know this seems like prehistoric iOS code, but I am completely at a loss here and have wasted hours fiddling in Xcode and IB. I know I am missing something obvious.
It seems like the height of your table view is set to be a fixed height where the toolbar is visible beneath the table view on iPhone 5 and up. To get the toolbar to display on iPhone 4/4s requires reducing the height of the table view's frame using something like the following code.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (onIphone4 && portraitOrientation) {
CGFloat height = 480 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 4/4s
} else if (onIphone5 && portraitOrientation) {
CGFloat height = 568 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 5 and up
}
}
You will probably need to change the y position in the frame origin to account for the status bar and navigation bar if they are also present. In that case, the overall frame height will need to be adjusted for those changes. This is the frame-based way of adjusting view sizes.
The alternative and preferred method is to add auto layout constraints in the XIB for the toolbar and the table view in Interface Builder so that they will maintain the proper size and position relative to the screen dimensions.
I found the issue.. it was the "Full Screen at Launch" checkbox in IB, for the main "UIWindow"... and I quote from Apple documentation....
"If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch option in the Attributes inspector so that the window is sized appropriately for the current device."
Now please excuse me while I slam head against wall..way to waste a Sunday!
I am creating an iPhone app, now i am trying to align a uilabel inside custom tableview cell to the right using autoresizing mask, i am doing it in interface builder and i am using storyboards, but the problem i am having is that the label just disappears when i run app in iphone 6 simulator, the autoresizing is working fine when i align label to the left of table view cell, but strangely it is not working when i try to align the label right of table view cell, how should i do this?
Note: i don't need to align the uilabel text but need to align the uilabel itself to right.
the answer is used for my project , please customize the x, y , width and height as you need
I also face the same problem , SO i follow this method cellForRowAtIndexPath
cell.yourlableName.frame=CGRectMake(add your label x - position,add your label Y position, [[UIScreen mainScreen] bounds].size.width- add your label width, add your label Height ); for example in your iPhone 4 width is 320 so reduce the width 320-yourlabel width,
else part call this line in your custom cell class
- (void)awakeFromNib {
cell.yourlableName.frame=CGRectMake(180.0f,220.0f, [[UIScreen mainScreen] bounds].size.width-216.0f, 19.0f);
}
its working fine for me in all device it shows in right side perfectly.
I'm switching to autolayout and I'd like to position views relatively to height of device. How should I setup constraints to satisfy such condition.
I have nice layout for iPhone 5 but for iPhone 6Plus I'd like to move "red" to position of "gray":
All my current constrains:
One idea might be to place the username, password and login items on a uiView with a clearBackground and then create a constraint for that view and its superview and create an outlet to it. You could then detect which phone you are using in code and modify the constraints programatically in willLayoutSubviews.
if ((int)[[UIScreen mainScreen] bounds].size.height == 736)
{
// This is iPhone 6+ screen
myConstraint.constant = 150;
} else if ((int)[[UIScreen mainScreen] bounds].size.height == 568) {
// This is iPhone 5s screen
frameRateLabelHeightConstraint.constant = 100;
}
There will be a better way to do this in Autolayout no doubt but I do find it a bit confusing so I have used this method in the past.
Here is a sample project uploaded # One Drive. Here are the sample outputs on various versions of iPhone Simulators...
As I see you want to put your form to the center of view. So I think the best solution will be to put your form elements on another transparent view and add to this view centerX aligment constraint and centerY aligment constraint.
you can check another way here
i am using autolayout in my project. I added the contraints from the bottom to the top. Everything works fins in 4 inch screen . But screen is clipped when the project is run on 3.5 simulator.
Looking at the screenshots, it looks like there is no space to fit all your elements.
One way to go about it is, dock the elements at the top of the screen using the Top Space To Superview constraint, and dock the elements at the bottom using the Bottom Space To Superview constraint.
When you do this, if there's not enough space available, you'll probably see some of your subviews overlapping around the middle.
You can consider solving this by using a UIScrollView as a top level subview and add all your subviews to the scrollView. The content height of the scrollView will be calculated based on your constraints.
You can also manually set the contentView height after viewDidLayoutSubviews is called.
Personally I find that sometimes I can't seem to get Autolayout to work exactly as I want. So its worth putting a check in your viewDidLoad to manually move objects around a bit if the screen size is a 3.5 inch screen like so:
In my example I will be using a Button:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height == 480) {
// 3.5 inch display
button.frame = CGRectOffset(button.frame, 0, 89.0f);
}
if (result.height >= 568) {
// 4 inch display
// .....
}
}