How to adjust height of table view programmatically in viewdidload - ios

How can I adjust my tableview height programmatically in viewdidload? actually I have a view in my app I want that whe view will load then uivew will be hide ang tableview height will be increase? How it can be done? I had added these lines in viewdidload but nothing happens?
- (void)viewDidLoad {
[super viewDidLoad];
CGRect tableFrame = [tableListView frame];
tableFrame.size.height = 127;
[tableListView setFrame:tableFrame];
}
Below is my screen shot I want to hide that greycolourview and want to increase my tableview height.

Give this a shot:
1.
This should be your constraints on the tableView:
2.
//declare a constraint outlet and connect it
IBOutlet NSLayoutConstraint *constraintGrayViewBottom;
3.
This is the bottom constraint of that gray view i.e: constraintGrayViewBottom.
Select the bottom constraint as shown and on the right, click-drag the referencing outlet connection to the viewController and connect it to constraintGrayViewBottom.
4.
Handle the constraint changes as example:
-(void)viewDidAppear:(BOOL)animated {
constraintGrayViewBottom.constant = -60;
[UIView animateWithDuration:1
animations:^{
[self.view layoutIfNeeded];
}];
}

Related

How to use autolayout to allot full frame to topview when bottom view is hidden

I have two UIViews aligned vertically in Main view, there is business logic like if usertype A is logined need to show both views, If usertype B logined need to show top view only and hide that bottom view but topview should get total frame of bottom view aswell, Can anyone please suggest me how can implement with Autolayout ?.
You should define height constraint for your bottom view.
Main idea is to change that constraint for your purpose. Just create outlet for height in your code and set it constant to 0, when you need to hide it (optionally, set hidden property to YES, if you wish)
Storyboard steps:
Add leading, trailing, top and bottom constraints for top view
Add leading, trailing, height and bottom constraints for bottom view
Your constraints should look like:
Last step - drag outlet for height to your view controller in order to get access to change it's constant
Hope this helps
If your deployment target is iOS 9 or later, the easiest way to do this is to put the views in a UIStackView:
I put the two views (plus the toolbar) in a vertical stack view. I constrained each view's leading and trailing edges to the stack view, and constrained the top and bottom views to have equal heights. Then I set the priority of the equal-height constraint to 249 (a fairly low priority). If you don't want equal heights, you can set whatever height constraints you want for when both views are visible. Just set them to a low priority so the stack view can override them when only one view is showing.
Hiding and showing the views is easy, even with animation. It's this simple:
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIView *topView;
#property (strong, nonatomic) IBOutlet UIView *bottomView;
#end
#implementation ViewController
- (IBAction)hideTop:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
self.topView.hidden = YES;
self.bottomView.hidden = NO;
}];
}
- (IBAction)hideBottom:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
self.bottomView.hidden = YES;
self.topView.hidden = NO;
}];
}
- (IBAction)showAll:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
self.topView.hidden = NO;
self.bottomView.hidden = NO;
}];
}
#end

How to change the size of tableview programmatically?

I try to change the size (the width) of a table viewcontroller (the panel with text over syria):
I have tried :
-(void)viewWillAppear:(BOOL)animated
{
self.tableView.frame = CGRectMake(0, 310, self.view.frame.size.width,200);
}
but it doesn't work. Do you know how can I reduce the width of the TableView ?
Thanks for your help !
You will have to add the table view as a subview to your view controllers view with the new size in viewWillAppear
Check the view frame width to make sure it is actually less than 320 before setting it. Most likely you are setting the table view width back to 320 which is the entire width.
Also if you are just setting the width of the table I would recommend doing the following.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.width = 250;
self.tableView.frame = tableViewFrame;
}

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.

iOS View is not expanding the way I want it

I have a UIViewController with a UICollectionView and a UIView at the bottom. The way I put it together is displayed in the image below
The yellow square is the UICollectionView and the red is the UIView. This works out just fine. But now I want to resize the UIView because it sometimes contains more info and needs to be bigger. So I tried this:
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height + 10)];
But this expands the UIView at the bottom and it is not visible. I guess this is because I do not have the correct constraints? I also tried to subtract the origin.y with the same amount and this works only the UICollectionView doesn't get resized with the new height. So how do I tackle this problem?
If you are using autolayout, you should not be setting the frame from your code. Instead you should modify the constant of a constraint that is causing your view to be the incorrect size. You can have an IBOutlet to that constraint and you can change it's constant property. Then call setNeedsLayout on your view controller's view
When setting constraints on your storyboard or in a xib file, animations perform animations on the constraints instead of the sizes and positions.
First create a outlet reference of the constraint which will change (in your case the top space of your UIView to the top layout guide) in the header file of your view controller.
When you want to animate a view, you now have to update its constraints and ask to layout the views.
For example :
[UIView animateWithDuration:0.2
animations:^{
viewYConstraint.constant -= 44;
[self.view layoutIfNeeded];
}
]
//Now don't forget to update constraints
[self.view updateConstraints];

UIScrollView wrong offset with Auto Layout

I have a fairly simple view configuration:
A UIViewController, with a child UIScrollView and a UIImageView in this UIScrollView.
I set the UIImageView with a height sufficient to break out of the visible area (ie. higher to 1024pt), and set the Bottom space to superview constraint of my UIImageView to a fixed positive value (20 for example).
The whole setup works as expected, the image scrolls nicely in its parent.
Except when the view is scrolled (the effect is more visible if you scrolled to the bottom of the view), then disappear, and appear again (you switched to another view and came back) the scrolling value is restored, but the content of the scroll view is moved to the outside top part of its parent view.
This is not simple to explain, I'll try to draw it:
If you want to test/view the source (or the storyboard, I did not edit a single line of code). I put a little demo on my github: https://github.com/guillaume-algis/iOSAutoLayoutScrollView
I did read the iOS 6 changelog and the explanation on this particular topic, and think this is the correct implementation of the second option (pure auto layout), but in this case why is the UIScrollView behaving so erratically ? Am I missing something ?
EDIT: This is the exact same issue as #12580434 uiscrollview-autolayout-issue. The answers are just workarounds, as anyone found a proper way to fix this or is this a iOS bug ?
EDIT 2: I found another workaround, which keep the scroll position in the same state the user left it (this is an improvement over 12580434's accepted answer):
#interface GAViewController ()
#property CGPoint tempContentOffset;
#end
#implementation GAViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tempContentOffset = self.mainScrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentOffset = self.tempContentOffset;
}
This basically save the offset in viewWillAppear, reset it to the origin, and then restore the value in viewDidAppear. The problem seems to occur between these two calls, but I can't find its origin.
Yeah, something strange happened with UIScrollView in pure autolayout environment. Re-reading the iOS SDK 6.0 release notes for the twentieth time I found that:
Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
Solution
Connect your subview to the outer view. In another words, to the view in which scrollview is embedded.
As IB does not allow us set up constraints between the imageView and a view outside the scroll view’s subtree, such as the scroll view’s superview then I've done it in code.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view removeConstraints:[self.view constraints]];
[self.scrollView removeConstraints:[self.scrollView constraints]];
[self.imageView removeConstraints:[self.imageView constraints]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[_imageView(700)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_imageView(1500)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
}
And vau! It works!
The edit didn't work for me. But this worked:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.tempContentOffset = self.scrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.scrollView.contentOffset = self.tempContentOffset;
}
For me I went to the IB clicked my view controller that contains the scroll view. Then I went to Attribute Inspector -> View Controller -> Extend Edges -> Uncheck "Under Top Bars" and "Under Bottom Bars".
Simple solution found, Just put
[self setAutomaticallyAdjustsScrollViewInsets:NO];
in your ViewControllers viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setAutomaticallyAdjustsScrollViewInsets:NO];
}
I had a similar problem using a UIScrollView in a UIViewController with a top extra space that was not visible in Storyboard. The solution for me was to uncheck the "Adjust Scroll View Insets" on the ViewController storyboard properties : see answer Extra Space (inset) in Scroll View at run time
Add a global property contentOffset and save the current contentOffset in viewDidDisappear.
Once you return the method viewDidLayoutSubviews will be called and you can set your original contentOffset.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.scrollView setContentOffset:self.contentOffset animated:FALSE];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.contentOffset = self.scrollView.contentOffset;
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:FALSE];
}
Looks like the problem solved with the dispatch_async during the viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGPoint originalContentOffset = self.scrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
dispatch_async(dispatch_get_main_queue(), ^{
self.scrollView.contentOffset = originalContentOffset;
});
}

Resources