I made a demo project for my problem. I added a tableview inside UIViewController. I set tableview constraint like this image. When I want to get this bounds and frame, I received picture 2. How I get tableView size like inside viewDidAppear ?
UIView has a frame property and since UITableView inherits from UIView you can just use tableView.frame to get its frame, and to get its width and height you can use tableView.frame.size.width and tableView.frame.size.height.
Please note that you will not get proper dimensions of your view in viewDidLoad but you will get proper width and height in viewWillAppear and viewDidAppear.
If you want the screen size you can always use UIScreen.mainScreen().bounds.height and UIScreen.mainScreen().bounds.width
Hope this helps.
Related
I have got a view and some elements in it
let background_img_view: UIView = {
let view = UIView()
return view
}()
background_img_view.addSubview(background_image)
background_img_view.addSubview(border)
background_img_view.addSubview(about_txt)
about_txt size is unknown, it can be 30px or 300px, now I want my background_img_view's height to depend on about_txt's height.
How can I make it happen programmatically?
Have you tried using the sizeThatFits(_:) method for the about_txt view. Use the size that this function returns and set the superview's size to that.
https://developer.apple.com/reference/uikit/uiview/1622625-sizethatfits
If you have the subviews added before the view is on the screen, you could set the height in the viewWillAppear method of your view controller. You can also update the size in viewWillLayoutSubviews.
If you're using auto layouts, have a look at setNeedsLayout vs. setNeedsUpdateConstraints and layoutIfNeeded vs updateConstraintsIfNeeded
I want to get my UITableViewController to scroll only when there is not enough room on the screen for all of its cells. I am using static cells that are designed to be shown in an iPhone 5, 6, and 6 plus size screen. However, when shown in the 4s screen, the bottom cells get cut off.
Ideally I would like to use AutoLayout to anchor the bottom of the tableview to the bottom of its superview as I have with other tableviews in my application, but Xcode doesn't allow me to add constraints to the UITableView in a UITableViewController. The reason I have to use the UITableViewController is because I am using a pod for a slide menu (https://github.com/SocialObjects-Software/AMSlideMenu) that subclasses UITableViewController.
I have tried to change the size of the UITableView's frame based on the screen size because I assumed a scroller would automatically be added if the cells took up more room than the containing frame. I used the following code to do so:
CGRect frame = self.tableView.frame;
[self.tableView setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [[UIScreen mainScreen] bounds].size.height - HEADER_HEIGHT)];
However, it had no effect on the way the table was displayed.
Does anyone know how I might be able to set the bottom of the UITableView dynamically and add a scroller to the cells when the screen is too small? I would also welcome any suggestions that might help avoid having to do this at all, as I would prefer not having to do anything too hacky.
You can set the alwaysBounceVertical property to false so the tableView will only scroll when its contentSize is larger than the table view's frame which you can constrain to your view however you like.
tableView.alwaysBounceVertical = NO;
UITableViewController height is determined by automatically calculating number of UITableViewCell you have presented. You can customize the cell height so that height of UITableViewController will automatically changed as you wished.
I subclassed UIView: http://pastebin.com/MVk1XmPS and when I add it to another view of a UIViewController:
self.myView = MyView.newAutoLayoutView()
self.myView.backgroundColor = UIColor.redColor()
self.view.addSubview(self.myView)
the red background is missing. It seems that myView has no width and height. However the content is visible:
I have to hard code the dimension:
self.myView.autoSetDimensionsToSize(CGSize(width: 100, height: 100))
but shouldn't it grow automatically by the size of the content? I use swift, xcode6.1 and iOS8.1
Views do not automatically grow to fit their content. If you enable clipsToBounds on your view, you will see the content disappear. Alternatively, if you put a button as part of your content, you will see it but be unable to interact with it because it is outside of its superview's bounds.
In your subclass, you could implement the sizeToFit or sizeThatFits: methods, but these are not going to be automatically called by the system. You should consider assigning constraints for your view in your storyboard. If you're not using a storyboard, you will need to assign a frame in your viewWillLayoutSubviews or layoutSubviews method.
Storyboards will also like your custom view better if you implement intrinsicContentSize.
In a UIViewController on a storyboard, I have a UITableView that is sized specifically to have two rows in one section with no header or footer, i.e. the height is 88.0f. There are some cases when I want to add a third row. So in viewWillAppear:animated: (and other logical places) I set the frame to be 44.0f logical pixels higher:
CGRect f = self.tableView.frame;
self.tableView.frame = CGRectMake(f.origin.x, f.origin.y, f.size.width, f.size.height + 44.0f);
NSLog(#"%#",NSStringFromCGRect(self.tableView.frame));
Nothing controversial; pretty standard resize code, and yet... It doesn't work! The tableView height doesn't change visually. The NSLog statement reports the height I expect (132.0f). Is this because I'm using Storyboards? I'm not sure why this isn't working.
Set an auto layout constraint for the height of the table view in your storyboard. Then connect the constraint to an outlet in your view controller so you can access the constraint in your code. Have the constraint be set to 88. When you want to change the height of the table view, just change the constraint's constant to 132.
You can modify the frame only after the call to layoutSubviews is made, which occurs after viewWillAppear. After layoutSubviews is called on the UIVIew you can change the dimensions.
As Gavin suggests, if you have the autolayout enabled you can add the constrains to the UITableView via storyboard, connect the height constraint and modify its value as follow:
constraint.constant = 132.0f
Otherwise if you have the autolayout disabled you can simply change the frame updating the height, but putting the code in a different method, for example viewDidLoad:.
recently I'm try to do what you've do. And I got same problem, tableview height won't change. Now I got the solution, you need to call layoutSubviews after change the frame. And it work on me.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height + 44.);
[tableView layoutSubviews];
}
don't place it in viewDidLoad or viewWillAppear: because even layoutSubviews is called, the frame won't change. place it on viewDidAppear:
I am using a UICollectionView with fixed size CollectionViewCells. I display this CollectionView in a popOver with an UIPopoverController.
My goal is that the popOver automatically resize to the CollectionView's content. So if I only have 4 cells, than it should be small. And if I have more cells, it should be bigger. I want to avoid the empty space left in the popOver.
So I don't want a fixed popOver size, but a variable one depending on the number of cells in the CollectionView.
Any suggestions?
I had a hard time figuring this out myself but just got it finally. You have to access your Collection View's collectionViewLayout property and then use the collectionViewContentSize method to get the size of your content. You can then use those to resize your Collection View by setting it's frame.
CGRect rectangle;
rectangle = _collectionView.frame;
rectangle.size = [_collectionView.collectionViewLayout collectionViewContentSize];
_collectionView.frame = rectangle;
Now that your collection view is sized to it's content, you can set the popOver's size based on the collection view size. It may look something like this.
popOver.frame = _collectionView.frame;
or
[popOver sizeToFit];