Change size of views inside ContainerView programmatically - ios

I have a container view which is equally divided between two UIViews like this:
The portion in black is my UIView 1, which I am currently not using. My UIView 2 contains a UISegmented Bar and a UITableView.
The hierarchy of my views look something like this:
Now, my requirement is I want to resize my view2 to cover entire container view dynamically and view1 to go away based on some condition. Currently I am not worried about that condition, I just tried to resize my view2 using the following code inside - (void)viewDidLoad
CGRect newFrame = self.view2.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
newFrame.origin.x=0;
newFrame.origin.y=0;
[self.view2 setFrame:newFrame];
Here view2 is the outlet to my view2 in interface builder.
But, nothing is changing from the above code. I tried to find any other way but had no help. So please help me find out my mistake in my current technique or tell me how some other technique to do it.
Thanks in advance!

The code to change frame seems fine, just remove the view from superview, set the frame and add to superview again programmatically.
[self.view2 removeFromSuperview];
// set the frame
CGRect newFrame = self.view2.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
newFrame.origin.x=0;
newFrame.origin.y=0;
[self.view2 setFrame:newFrame];
//add self.view2 again wherever it was
[myView addSubView:self.view2];

Related

How to animate imageview from off screen

I have an imageview I put onto the view controller of a circle.
I would like to start that circle from off screen and move it to the location I've put it.
What would be the best way to animate it?
Try this;
-(void)viewWillAppear {
[UIView animateWithDuration:5
delay:5
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect frame = view.frame;
frame.origin.y = self.view.frame.size.height-200;
frame.origin.x = 0;
view.frame = frame;
}completion:^(BOOL finished) {}];
}
You many animation option's [UIViewAnimationOptions]
UIViewAnimationOptionCurveEaseInOut,
UIViewAnimationOptionCurveLinear,
UIViewAnimationOptionTransitionFlipFromLeft,
UIViewAnimationOptionTransitionCurlUp,
UIViewAnimationOptionTransitionCrossDissolve,
UIViewAnimationOptionTransitionFlipFromTop..
You want to create your image view off-screen. Its easiest if you set it up in Interface Builder that way. you can create it on-screen, add a constraint to place it at the target location, note the value, and then change the constraint's constant so the view is moved off-screen.
Then control-drag the constraint into your view controller and create an outlet to it.
To animate the image view on-screen, change the constraint's constant value to the "on-screen" value you noted above, and then call layoutIfNeeded() on the view from inside an a UIView animateWithDuration block.
If you want to add the image view in code then you need to add it off-screen, add it as a subview, and then trigger a UIView animation to move it back on-screen. You should probably also do the positioning with constraints as outlined above, although it gets more complicated because you have to create those constraints in code.

Change frame of UIView and update the constraints of it childrens

I need to change UI element based on what button was pressed, basically it is like a tab, but in just one part of my View Controller. The elements that I need to show/hide are UIView with multiple collection views and labels inside.
What I am doing right now Is defining both objects int he IB and programatically, when I need to show one I just set the width of one of them to 0 and set a new frame for the other one with the whole width I want. The problem with this approach is that even what the UIView is as I want it to be, the contents inside seem to keep the previous constraints (centered) and they don't "adapt" to the whole width.
Current Code:
var newFrame = self.view.frame
newFrame.origin.x = 0
newFrame.origin.y = self.blueView.frame.origin.y
newFrame.size.height = self.blueView.frame.size.height
if (showGreen){
self.greenView.frame = newFrame
self.greenView.hidden = false
self.blueView.frame.size.width = 0.0
self.blueView.hidden = true
}else{
self.blueView.frame = newFrame
self.blueView.hidden = false
self.greenView.frame.size.width = 0.0
self.greenView.hidden = true
}
In the IB I set the constraint for both views to have no separation between them and the margins.
I would like you to help me either in fixing this issue or with another way of achieving the desired behaviour behaviour.
Thank You.
you should change constraint.constant value, not the frame of UIView's,
make an IBOutlets for constraints you need, and change it this say:
self.widthConstraint.constant = newValue;
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];

Why is my UIView's frame changing unexpectedly after the embedded UITableView is interacted with?

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.

How to change View size programmatically?

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

UITableView not scrolling enough after expanding subview

I have a UITableViewController with a UIView at the bottom. (using storyboard). My UIView is set to hidden and changes state afterwards on click of button. First, I was trying to resize (increase height basically) my UIView on IBAction(buttonClick) using self.concluidoView.frame = CGRectMake(0, 0, 320, 50); but UIView was disappearing instead.
Now, it is correctly expanding UIView with the following code inside IBAction:
[UIView animateWithDuration:1.0
animations:^{
CGRect frame = self.concluidoView.frame;
frame.size.height += 100.0;
self.concluidoView.frame = frame;
}
completion:^(BOOL finished){
// complete
}];
The problem is that my UITableViewController is not scrolling enough to the new size, since the UIView is the last item in my tableView.
I've tried a few things to solve this problem, including manually trying to resize tableview to increase it's height to the same value of the UIViewincrease. I used the following code:
CGRect tableFrame = self.tableview.frame;
tableFrame.size.height += 100;
self.tableView.frame = tableFrame;
[self.tableview layoutIfNeeded];
The scrolling capacity of my tableviewwas actually smaller after this code. I would like to resize tableviewand allow scrolling, either manually, since I know how much my subviewwill increase, or automatically.
If you change the UITableView's frame, all you'll do is make it extend off screen most likely. What you want to do is get the table view to recognize that the UIView is larger. I'm assuming this UIView is the tableFooterView of your UITableView. Try doing the following:
UIView *footerView = self.tableView.tableFooterView;
self.tableView.tableFooterView = nil;
self.tableView.tableFooterView = footerView;
That will force the UITableView to reexamine the size of the footer view. I've had to do this before when changing the size of a footer view before.

Resources