UIView's frame rendering larger than frame - ios

I am trying to add a subview to a UIScrollView. First I instantiate the view controller from the storyboard, then set the view's frame to the application bounds. When I add the view to the UIScrollView, it is clearly larger than it's supposed to be.
CGRect mainFrame = CGRectMake(0, topButtonHeight, screenWidth, screenHeight);
feelingVC = (FeelingViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"feelingVC"];
feelingVC.delegate = self;
feelingView = feelingVC.view;
[feelingView setFrame:mainFrame];
[self.scrollView addSubview:feelingView];
I can tell because its background color is extending past where it should be. "Debug View Hierarchy" mode in XCode also confirms this. But, if I inspect the view's frame, it prints out what it should be, not what it actually is.
A view of the same size, but generated completely programatically, works as it should:
mainView = [[UIView alloc] initWithFrame:mainFrame];
mainView.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:mainView];
I'm not sure what could be causing this issue - I've instantiated other views from their view controllers, also via the storyboard, and set their frames without any problems.
EDIT: This is being called from viewDidLoad of the view controller containing the UIScrollView. screenWidth & screenHeight are calculated as such (they are instance variables):
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;

Try to set the view frame in viewWillAppear method.
viewDidLoad is not a good place to set frames (because it called only once when view getting loaded.), the UI components are not properly configured by the system yet.
Also, prefer to use:
screenWidth = [UIScreen mainScreen].bounds.size.width;
screenHeight = [UIScreen mainScreen].bounds.size.height;
instead of
screenWidth = [UIScreen mainScreen].applicationFrame.size.width;
screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
since bounds has correct positional information taking into consideration the device's orientation in this case.
Or you can just do this, after initializing the mainView inside viewDidLoad: Method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
mainView.frame = [UIScreen mainScreen].bounds;
}
you can also add this to reconfigure the view frame whenever the subviews are updated:
- (void)viewWillLayoutSubviews { //or viewDidLayoutSubviews
[super viewWillLayoutSubviews];
mainView.frame = [UIScreen mainScreen].bounds;
}

Related

Centre align Subview UIview at the centre

I have following storyboard based design with constraints
I am adding this view to main view as follows
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:#"CompanyFundView" owner:nil options:nil];
CompanyFundView *view = [nibContents lastObject];
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
UIView *parent = mainWindow.subviews[0];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
[parent addSubview:view];
As you can see the view isnt appearing inside the main view and not getting resized , what are the other constraints i need to add ?
Make sure you are adding NSLayoutConstraint on a storyboard scene with width and height set to w:Any h:Any
I can see you are using w:Any Regular
The constraints are specific so it won't scale.
Use specific scene sizes when dealing with specific layout for a targeted device size

Change frame of UIView created in interface

My app was designed for iPhone 5, but now I'm adding compatibility for other devices.
I have a UIView, created in Interface Builder. I want to set a new frame for this view. I am checking the screen's width and height at runtime, but it's not changing the view's frame. Here is my code:
//in viewDidLoad:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
if (screenHeight == 480 && screenWidth == 320) {
bottomView.frame = CGRectMake(0, 60, 320, 420);
}
Goto interface builder tap on view, you want to resize, click on Attribute inspector than click on size and select freeform after that in your viewWillAppear resize your view, you can check size of you view by:-
NSLog(#"view frame ==> %#", NSStringFromCGRect(self.view.frame));
Make a class that returns you a screen frame info.
Create a new File with name FullScreen with UIScreen as sub class
#import <UIKit/UIKit.h>
#interface FullScreen : UIScreen
+ (CGRect)iOS7StyleScreenBounds;
#end
#import "FullScreen.h"
#implementation FullScreen
+ (CGRect)iOS7StyleScreenBounds {
UIScreen *screen = [UIScreen mainScreen]; CGRect screenRect;
if (![screen respondsToSelector:#selector(fixedCoordinateSpace)] && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
screenRect = CGRectMake(screen.bounds.origin.x, screen.bounds.origin.y, screen.bounds.size.height, screen.bounds.size.width);
} else {
screenRect = screen.bounds;
}
return screenRect;
}
#end
Now import this class in your view controller and call like this:
self.view.frame = [FullScreen iOS7StyleScreenBounds];
Try Putting the code in
- (void)viewDidAppear:(BOOL)animated;
One definite solution which can work is , you should remove your UIView from xib and create it programmatically.Which can be done by the following code:
newView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[newView setBackgroundColor:[UIColor yellowColor]];
[self.view newView];

Change the width of a popover which has table view in it

I'm trying to achieve two things:
To change the height of the popover to the number of cells in my static table view (which works).
But for the popovers width to be configurable using the storyboard builder.
- (void)viewDidLoad
{
CGRect popoverBounds = self.view.frame;
[self.tableView sizeToFit];
CGRect newBounds = self.tableView.bounds;
newBounds.size.width = popoverBounds.size.width;
self.tableView.bounds = newBounds;
self.preferredContentSize = self.tableView.contentSize;
}
currently this sets the popover to the width of the table view.
Problem is inside this line:
self.tableView.bounds = newBounds;
You change internal bounds, instead of frame, you need to use:
self.tableView.frame = newBounds;

How to I resize an ImageView in the ViewDidLoad?

I have an imageView that is resized in two different parts of my code. This works great in my textFieldDoneEditing. However, when I use the same code in my viewDidLoad, I get a different sized view. Is there a difference with doing this in the that method?
Here's the code:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGRect frame = CGRectMake(0,0, screenWidth, screenWidth);
PuzzleImage.frame = frame;
Thank you
If your ViewController is landscape mode, viewDidLoad always gets main screen's bounds in portrait mode.
So, solution is setFrame in viewWillAppear to get right bounds.

Incorrect size of a textview frame (self.textview.frame.size.height)

I want to change the height of a textview depending on its content. I created the method to resize its view. The first time I call this view controller it resize properly (height = 253) but not the other times (height = 296).
I tried resizing it from viewDidAppear, viewWillAppear and viewDidLoad. The first time viewDidAppear and viewDidLoad are called. Second and following times all methods (viewDidAppear, viewWillAppear and viewDidLoad) are called. I don't know the reason and I don't know why this weird behavior, any clue?
-(void) setHeight
{
NSLog(#"Set height");
CGRect frame = descriptionTextView.frame;
frame.size.height = descriptionTextView.contentSize.height;
descriptionTextView.frame = frame;
NSLog(#"height: %f", descriptionTextView.frame.size.height);
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
totalHeight = 380+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
else{
totalHeight = 250+frame.size.height;
[self.mainScrollView setContentSize:CGSizeMake(320,totalHeight)];
}
}
I use autolayout in my project but not for this view since I dont know how to properly resize a textview inside a scrollview (which also includes 2 more views with labels, images and buttons) based on the textview content with autolayout. Is it better to use autolayout than this function? Perhaps you can help me with the constraints...
You are changing only the scroll size according to your calculation but you are missing to change the size of text view itself. After setting scroll view content size update the size of the text view and all should be fine (contentSize and frame). This may look like:
[descriptionTextView setContentSize:CGSizeMake(CGRectGetWidth(descriptionTextView.frame),totalHeight)];
CGRect frame = descriptionTextView.frame;
frame.size.height = totalHeight;
[descriptionTextView setFrame:frame];
By the way: in the if-statement you can only calculate totalHeight and set it afterwards.
Additionally you do not need this line:
frame.size.height = descriptionTextView.contentSize.height;
Try this:
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = yourTextView.frame.size.width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"yourFontName" size:yourFontSize], NSFontAttributeName,
nil];
CGRect frame = [yourTextView.text boundingRectWithSize:constraintSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
// Use this frame for your textview

Resources