I have a custom keyboard using an interface is laid out in a .xib file (which does not use auto-layout). At the moment, I am having trouble extending the height of the interface to be more than the default 216 points.
I have attempted to constrain the height of the keyboard by using Apple's suggested method NSLayoutConstraint constraintWithItem: self.view..., but it only causes the keyboard to not appear at all.
This is how I initialize the interface in my KeyboardViewController.m (a subclass of InputViewController)
[[NSBundle mainBundle] loadNibNamed:#"CustomKeyboardLayout" owner:self options:nil];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width; //these have no effect in determining the height/width of the keyboard
CGFloat screenHeight = screenRect.size.height;
self.keyboardOverlayView.frame = CGRectMake(0, 0, screenWidth, screenHeight);
Related
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
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;
}
I am attempting to make a UIView that is constrained to the bottom of the container view without actually doing auto layout an constraints. Here is my code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenWidth / 2), screenWidth, screenWidth / 2);
}
I was hoping this would create a subview that would appear pinned to the bottom of the screen and be the width of the screen and half that tall. However, when the view loads, there is some space between my graphView and the bottom of the screen. Any clue why this is?
I think you are using screenWidth when you need use screenHeight
The correct code:
-(void)viewDidLoad
{
[super viewDidLoad];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self.graphView.frame = CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2);
}
The solution: Turn off auto layout and any other IB toggles that have an effect on where the view is being placed. The original code is correct for the rectangle I wanted, it was just being moved by auto layout. Also it's better to set up the graph container like this:
self.graphContainer = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight - (screenHeight / 2), screenWidth, screenHeight / 2)];
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.
I have scrollView set for horizontal scrolling (paging enabled, ...).
If i create UiViews in code and add them to scrollView everything works ok.
Example:
//second page
CGRect frame2;
frame2.origin.x = myScrollView.frame.size.width;
frame2.origin.y = 0;
frame2.size = myScrollView.frame.size;
UIImage *tutImage2=[UIImage imageNamed:#"tutorialScreen2.png"];
UIImageView *tutorialImage2=[[UIImageView alloc] initWithImage:tutImage2];
tutorialImage2.frame=tutImageFrame;
UIView *subview2 = [[UIView alloc] initWithFrame:frame2];
subview2.backgroundColor = [UIColor clearColor];
[subview2 addSubview:tutorialImage2];
[myScrollView addSubview:subview2];
The problem is with views loaded from xib, because they draw over scrollView (eventhough their frame is set to height smaller than full screen).
Example:
//third page
CGRect frame3;
frame3.origin.x = myScrollView.frame.size.width*2;
frame3.origin.y = 0;
frame3.size = myScrollView.frame.size;
dashView = [[[NSBundle mainBundle]loadNibNamed:#"DashboardView"owner:self options:nil] lastObject];
dashView.frame=frame3;
[myScrollView addSubview:dashView];
I tried setting my view's simulated metrics size to freeform and retina 4inch and it didnt help. I also turned off autolayout (i dont use it in my xib) but it also didnt help.
Any idea?