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
Related
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/
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?
I am trying to create a view that has 4 children
1 imageview
3 labels
The ImageView is supposed to take up the entire frame, and it doesnt
I've added layout constraints to it so that it would adjust to the sides.
However, i am initiating the view with different frames, and the image view size doesnt change
How do i make the image take up the entire purple area, of which it is a subview?
This is my xib file:
and this is how it looks on the device screen
the purple view is the frame i allocated for the given view:
and the green view is the entire screen
my init code:
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
[self addSubview:topViews[0]];
[self.shoppingBag setFrame:self.bounds];
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
UIView *v = topViews[0];
v.frame = self.bounds;
[self addSubview:v];
fixed the problem
Set the contentMode property of UIIMageView to UIViewContentModeScaleToFill
[your_imageview setContentMode: UIViewContentModeScaleToFill];
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
First, I have a exist Asset.m/Asset.h, it is the subclass of UIView. Then I create a empty xib file and change xib's file's owner class to Asset. Then I create a view and a label to this xib, and I can drag and create IBOutlet in UIView.
IBOutlet UIView *videoOverlay;
IBOutlet UILabel *videoTimeLable;
Then I want to add videoOverlay to Asset.m's view using addSubview.
self = [super initWithFrame:CGRectMake(0, 0, 0, 0)];
[self addSubview:videoOverlay];
But the view -- videoOverlay do not show up.
I print out size of videoOverlay, all 0:
NSLog(#"width =%f height= %f", videoOverlay.frame.size.width, videoOverlay.frame.size.height);
Why ?
Finally, I find the solution by myself. Use under code to add this view (videoOverlay and videoTimeLable belong to this view )in:
UIView *thumbs;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ELCAsset" owner:self options:nil];
thumbs = [nib objectAtIndex:0];
[self addSubview:thumbs];
then videoOverlay and videoTimeLable can show up.