I want to set the frame of a subview manually.
So I just create a CGRect with CGRectMake and use the new CGRect for the frame of the subview. The problem is that the subview don't show up.
When I just use the view.bounds property of the parent view and assign this as frame to the subview then everything shows up.
I also ensured that the frame is in the displayed area.
frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIView alloc] initWithFrame:frame]];
Good way to set subview's frame is
subview.frame = CGRectMake(0, 0,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds));
Refer to link
All the values less than CGRectGetWidth(self.view.bounds) & CGRectGetHeight(self.view.bounds) should work fine
The code in your question will create a view to which you have no reference. This means it has no background colour, contents or other means to see it, and you have no means to set it. You've added the view, but it is invisible.
Create and configure the view first, assigning it to a local variable, before adding it as a subview. As part of this configuration, give the view a background colour or some subviews.
Related
Consider the following UIView "MainView":
The view includes a Container View which in turn houses a UITableView controller. The container view's y coordinate starts just beneath the gradient bar. The UITableView includes the section footer at very bottom with the 'STU' label and 'chart' button.
When the UIView loads, and up-to-and-until any interaction with the tableView, MainView's dimensions are:
Frame: 0.000000x, 0.000000y, 568.000000w, 268.000000h
I have a delegate protocol set up such that tapping the chart button in the tableView will create a new view in MainView for a shadow effect via a method performing:
CGRect newFrame = self.view.frame; // self = MainView
newFrame.size.width = 100;
newFrame.size.height = 50;
UIView *backgroundShadowView = [[UIView alloc] initWithFrame:newFrame];
backgroundShadowView.backgroundColor = [UIColor blackColor];
// Do Animation
The important part above is the 'newFrame' CGRect. For some reason after interacting with the table view by tapping the chart button, or even scrolling or tapping a row, self.view.frame suddenly has the following dimensions:
Frame: 0.000000x, 52.000000y, 568.000000w, 268.000000h
And so the shadow view appears as follows, with a y origin much farther down than where it would be expected to start, just above the gradient bar.
I've adjusted the width and height of the "shadowview" for this question; normally it would be 568x268, but would extend 52 units off screen on the bottom because of this issue.
52 units is exactly the height of the statusbar (20) + navigationbar_in_landscape (32).
Of course I could manually adjust the frame dimensions, but I do not want to. I want to know why the view's frame is changing unexpectedly.
For the life of me, I cannot figure out why the view becomes suddenly offset. Any help is appreciated!!
Two comments.
(1)
This code was probably always wrong:
CGRect newFrame = self.view.frame; // self = MainView
newFrame.size.width = 100;
newFrame.size.height = 50;
UIView *backgroundShadowView = [[UIView alloc] initWithFrame:newFrame];
You surely want to define backgroundShadowView's frame in terms of self.view's bounds, not its frame as you are doing in the first line here.
(2)
The change in self.view.frame is probably illusory. You are probably checking this initially in viewDidLoad. But that is too soon; the view has not yet been added to the interface, and so it has not yet been resized to fit the surroundings.
There are few questions regarding this topic in stackoverflow. But none of the solutions are working for me.
I have a requirement to show pop up when user clicks on the row of a table view. Again this pop-up should contain a tableView. Since Apple recommandation is not use tableView inside a alertView,So, I need to use normal UIView with lesser size.
UIView is drawn using storyboard.
Programmatically I am trying following code to reduce the size.
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect newFrame = self.view.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
[self.view setFrame:newFrame];
}
But it is not working.
CGRect is not only width and height, its also x and y.
So do this:
CGRect newFrame = CGRectMake( self.view.frame.origin.x, self.view.frame.origin.y, 200, 200);
self.view.frame = newFrame;
This does only effect the width and height.
Probably the View that you are trying to modify is the one attached to the UIViewController via Storyboard, you will not be able to modify that one, try creating another one either with Storyboards or programmatically.
Since you are requirement is to display a popup , I would suggest you if its iPAD then go ahead with UIPopoverController and if its iPhone then just create a new full screen UIView with the transparent background (clear background) and then create one more UIView with new frames and make sure the frame size is in middle and attach it(Add subview) .
Also the procedure what you is doing won't work.Create a new UIView and change the frame and then attach or add subView to your main View
This will solve your issue.
I think you are looking for UIPopOverController with tableView..
Refer this tutorial
http://www.raywenderlich.com/29472/ipad-for-iphone-developers-101-in-ios-6-uipopovercontroller-tutorial
To set a UIView to take up the entire screen, is this correct?
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
Or should I be using the self.view.bounds...?
You can use either of frame and bounds for view as frame and bounds are same for it. But when you work with subviews then use bounds because frame and bounds need not be the same. For subview they are same only when your subview's size is same as its superview.
I am using GMGridView in my project. And in - (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index method, i write a snippet which is out of my expectation. The snippet is as follows:
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
ThumbImageView *view = [[ThumbImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[view setBackgroundColor:DEFAULT_BACKGROUND_COLOR];
[view setImage:[UIImage imageWithContentsOfFile:path]];
view.userInteractionEnabled = YES;
view.layer.shadowOffset = CGSizeMake(8, 8);
view.layer.shadowOpacity = 0.5;
NSLog(#"view frame width:%f,height:%f",view.frame.size.width,view.frame.size.height);
cell.contentView = view;
NSLog(#"cell.contentView frame width:%f,height:%f",cell.contentView.frame.size.width,cell.contentView.frame.size.height);
}
when it runs the output is as follows:
2013-06-03 11:02:05.508 XXX[71692:707] view frame width:115.000000,height:180.000000
2013-06-03 11:02:05.511 XXX[71692:707] cell.contentView frame width:0.000000,height:0.000000
why assign view to cell.contentView, cell.contentView.frame.size still be zero? and also cell.contentView can display the image properly. what's the reason? I am totally confused:(.
Check the docs:
contentView
Returns the content view of the cell object. (read-only)
#property(nonatomic, readonly, retain) UIView *contentView
contentView is a readonly property. You can't assign it.
The problem is probably that you've created new views, and you're looking at the frame of one of those views before the run loop has reached its layout phase.
When you create the GMGridViewCell, it starts with a default frame and bounds of CGRectZero. When you set its content view, it sets the content view's frame to its (the cell's) bounds. So even though you set the content view's frame directly using initWithFrame:, the frame gets changed to CGRectZero when you set the view as the cell's content view.
Since the cell isn't a subview of the grid view yet, there's no way to ask the grid view to lay out the cell by the time you're trying to log the content view's frame. If the grid view is using a constant size for all cells, and you know what that size is, you could manually set the cell to that size before setting the cell's content view. Otherwise, you need to wait until after the grid view's layoutSubviews method has run to check the content view's frame.
I have a custom view with some drawing in drawRect added as subview in a UIView.On tapping on the subview I change the height of the base view.This is done by -
CGRect rect = self.bounds;
rect.size.height = 400.0;
self.bounds = rect;
This also repositions the custom view (sets it to the top of the base view) which I don't want , the subview should remain fixed at the bottom of the UIView.
Y position for the custom view is set to the height of the base view -
y = self.bounds.size.height - 40.0;
This works when the custom control is added for the first time, but does not work when the base view changes height.
I have set self.autoresizesSubviews = NO; for the base view.
If I remove the custom view from the base view and add it again after the height changes nothing is shown , just a blank base view.
Any ideas to fix the position of the custom view to the bottom of the base view.
Try to use
self.bounds = CGRectMake(0, 0, 320, 400);
It will make the bounds from top left corner to point on the right side of the screen that is 400 pixels below the initial point.
You can also try using method
-(void)layoutSubviews
I don't remember the correct syntax, but I'm pretty sure it was like that.
hope it helps
What about firing setNeedsDisplay method on that view.