Adding subview designed for iPhone 7 looks big in smaller iPhone - ios

I have a problem when I add a Subview.
I have a main xib that works ok, and in it, I want insert a subview from another xib.
interface builder of subview xib with constraints
what I see in simulator in smaller iPhones
Both xib are designed for iPhone 7plus, I see them OK in iPhone 7plus simulator, but when I choose SE for example, I see the main xib perfect (in all iPhones), and the second subview xib I import in code, looks bigger than the screen.
(If in Interface builder, for de subview xib I choose iPhone SE, then I see it correct in iPhone SE simulator, but smaller in iPhone 7plus simulator)
I tried lot of constants with auto layout, but doesn't work. I think it must be a problem o code when I use insertSubview.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Crea vista Informacion
[self creaVistaInformacion];
}
-(void) creaVistaInformacion{
UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:#"AvisosInformacion" owner:self options:nil] objectAtIndex:0];
[[self view] insertSubview:viewInfoNib atIndex:0];
}
If you can help me, thanks!

You have internal constraint in the view described by your nib, but you do not have constraint external to the nib that position and size it in your superview (witch is the view of your view controller).
An example using frames :
UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:#"AvisosInformacion" owner:self options:nil] objectAtIndex:0];
// Setting the right frame (will not work if the view is resized)
[viewInfoNib setFrame:self.view.bounds];
[[self view] insertSubview:viewInfoNib atIndex:0];
An example using constraints :
UIView *viewInfoNib = [[[NSBundle mainBundle] loadNibNamed:#"AvisosInformacion" owner:self options:nil] objectAtIndex:0];
[[self view] insertSubview:viewInfoNib atIndex:0];
// Create here the constraints (will work even if the view is resized)
// You can create then with the visual format for example, check http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/

Related

Want xib file to cover whole screen instead of covering top left ios

I am making an ios app and using xib files instead of storyboards. At first I didn't even have to use auto layout everything worked fine for all iphone devices in portrait. But as soon as I added a launch screen through assests only the the iphone 5 has the correct layout (because xib file size is set to 4 inch). But any greater screen size and only the top left hand corner is covered like below:
I want the view to cover the whole screen. There is a view at the top but I cannot add any constraints to it. Inside the view there is an imageview that is supposed to cover the whole screen. So I added 4 constraints and pinned the 4 sides to 0 so the imageview should cover the whole screen. But it still does not work. What can I do so the whole screen will be covered?
Have you tried set the frame?
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[[array objectAtIndex:0] setFrame:self.view.frame]; // Trick here: set the main view frame
This worked in my case:
// Create a Container
UIView *container = [[UIView alloc] initWithFrame:self.view.frame];
container.backgroundColor = [UIColor clearColor];
// Add .xib file on top of the screen
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[container addSubview:[array objectAtIndex:0]];
// Add container to main view
[self.view addSubview:container];
About the itens inside of .xib file you have to set the constraints there.

Size classes not working when loading xib

I have a custom view, that I load like this:
MyCustomView *my = [[NSBundle mainBundle] loadNibNamed:#"MyCustomView" owner:self options:nil][0];
my.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:my];
//add constraints to make it fill superview
Problem is, on iPhone it only shows the size class Any|Any, ignoring all constraint changes I put in Compact|Regular.
Should I do anything else to make it read correctly the size classes on my xib file?

iOS8 auto layout with size classes works in preview but not in device/simulator

I am trying to use auto layout with size classes. I have a basic page with UIImageView as background and UIButton (with image as background). There are total of 6 constraints: 0 space for top/right/bottom/left for UIImageView and X center align and 230 for top space on UIButton. Here is what the interface builder looks like:
Her is what the preview looks like:
However, this is what I get when I run application on simulator (or device):
I cannot figure out why the constraints are not being executed?
I should mention that this behavior is experienced when I try to set up auto layout in the .xib file. If I try to do same thing in the project that has storyboard, everything seems to be in order for simulator and real device.
How are you loading the view from xib?
e.g. Assuming your view is in a file called View.xib, I can recreate your problem with
- (void)viewDidLoad {
[super viewDidLoad];
UIView *v = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil][0];
[self.view addSubview:v];
}
The reason this doesn't work, is because there is nothing telling auto layout to position and size the view to fill the screen, so the view just fits to its contents - which will be the size of the background image (because you have the constraints pinning the image to its parent view).
To fix it, first ensure the view is using auto layout by setting translatesAutoresizingMaskIntoConstraints = NO (it defaults to YES otherwise, when loading from XIB) and then pin the view to its parent:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *v = [[NSBundle mainBundle] loadNibNamed:#"View"
owner:self
options:nil][0];
[self.view addSubview:v];
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|"
options:0
metrics:nil
views:#{#"view": v}]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|"
options:0
metrics:nil
views:#{#"view": v}]];
}

Loading a View programatically in to a UIViewController

I have 2 questions actually. First I'll explain the scenario. I have a simple Storyboard app. The first controller is a UITableViewController displaying a list of mail types. When tapped upon a cell, it segues to a UIViewController and depending on the selected mail type, it loads a UIView into the UIViewController. This is all working fine. My problems are,
One UIView I have is 559 in height. I put a UIScrollView inside the UIViewController so that when the UIView with the extra height is loaded, it'll be scrollable. But instead of that happening, the UIView resizes to the size of the UIViewController. How can I keep its original height and embed it inside a UIScrollView?
From the Interface builder, I've put a UIToolbar at the bottom of the UIViewController. But when the UIView is loaded into it, the UIToolbar disappears. I guess the UIView appears on top of the UIToolbar(?). How can I avoid this from happening?
I know this must a bit confusing to understand. Therefore I've uploaded an Xcode project demonstrating the 2 issues here.
Please have a look and tell me what I should do.
Thank you.
I see a couple problems with what you're doing in your project that may explain why you're getting these results.
In NewMailViewController.m, you are replacing the view you've created in the storyboard (the one that has a scroll view and a toolbar) with the one you've loaded from the nib (i.e. self.view = [nib objectAtIndex:0];). Try doing this in -loadView
[super loadView];
UIView *someView = nil;
if ([self.mailTypeName isEqualToString:#"Email"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Email" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"Facsimile"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Facsimile" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"Memorandum"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Memorandum" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
else if ([self.mailTypeName isEqualToString:#"ProjectMemo"]) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ProjectMemo" owner:self options:nil];
someView = [nib objectAtIndex:0];
}
CGRect frame = someView.frame;
frame.size.width = self.myScrollView.frame.size.width;
[someView setFrame: frame];
[self.myScrollView addSubview: someView];
[self.myScrollView setContentSize: someView.bounds.size];
You'll notice that I have a reference to the scroll view in the above snippet of code. You'll want to create an outlet for the scroll view in your NewMailViewController class so that you can refer to it as needed. It involves putting this in your header file:
#property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
You'll need to connect the defined outlet to the actual storyboard's scroll view. You can search for "creating or connecting outlets in Xcode" on the Internet for more information about that.
The toolbar appears to be a subview of the scroll view. Don't you want the toolbar to rest at the bottom of the view while the content of the scroll view scrolls? If so, you'll want to move that toolbar view out from within the scroll view.
Here are the changes: http://www62.zippyshare.com/v/90505016/file.html

UIView from XIB-file: height is NULL

I have created a custom view with a XIB file and are loading the XIB as a UIView several times into my project fine.
But i want to know the size of the UIView that was created with the XIB File. (Not the best idea i know, but just to make sure i'm using the correct size even if the XIB file is resized later)
My code in the viewDidLoad from my UIViewController:
NSArray *nibObjects = [NSBundle.mainBundle loadNibNamed:nibName owner:self options:nil];
coverArticle *pageView = [nibObjects objectAtIndex:0];
[coverArticlesScrollView addSubview:pageView];
NSLog(#"View size: %#",pageView.frame.size.width);
The "pageView.frame.size.width" is returning NULL. Why is this ?
NSLog(#"View Size: %f", pageView.frame.size.width); width/height is of CGFloat type

Resources