Is there a way to manually set a UITableView's width?
I would like to set the table as a subview which takes up half of the width of the screen and the full height.
Sure you can set the table frame to whatever you want.
self.tableView.frame = CGRectMake(80, 0, 160, screenHeight);
Related
I have subview in a scrollview and after I remove some subview from the bottom there is a blank space and the scroll view still keeps height, not resizing. How to fix this?
Scrollview's contentHeight & contentWidth is calculated from its subviews(if used auto layout). If autolayout is not used then you need to set its content size.
So whenever you are adding or removing subviews in scrollview you need to take care of its contentHeight and contentWidth.
If you're using auto layout, you need to add constraints to newly added view in such a way that scroll view can calculate its contentHeight from it.(in auto layouts you don't need to explicitly set the content height to scroll view)
If you're using frame base layout, you need to set the contentHeight explicitly to the scroll view. In frame base layout, you need to take care of scrollviews content size.
Why is there blank space in scrollview?
When a subview is removed from scrollview, its content size need to be adjusted. It means it is not able to calculate its content size.
you can use this line
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: (YOURVIEW) - (THEREMOVEDVIEW))
I recommend you that you must save the height before remove view for later you can adjust.
UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself like below:
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: 700)
note: change width and height as you want.
you can try this also:
let content: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in
rect = rect.union(view.frame)
}
scrollView.contentSize = content.size
I have a viewController into which I dragged a tableView, but when I load data in the tableView I get a scroll, what I want is to extend this tableView to its height to fill its superView. I tried to resize the tableView by setting tableView.frame.height = tableView.contentSize.height in viewDidAppear and it worked but I don't get a scroll in the superView, the tableView just got expanded and the content is down the view but I cant scroll, I tried to put this tableView inside a scrollView but still the same thing, what can I do?
yourTableview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
Basically what you did was adjust the height of the tableView to be is the height if its content rather than the height of the screen (the content height is larger than the screen)
The table should carry the height of the screen (or any height you want) and if your content is bigger than the frame of your tableView it will automatically have the scrolling enabled.
Hope this helps!
If you want your scrollview to be scrollable, you need to indicate its contentSize by dynamically calculating the height of its nested components:
scrollView.isScrollEnabled = true
scrollView.contentSize = imageView.frame.height + tableview1.frame.height + tableview2.frame.height + netLabel.frame.height
Notice that you may also have to include your margins' height
I just needed to add the constraint of height of my table as an Outlet like this #IBOutlet weak var myConstraint: NSLayoutConstraint!, and the do myConstraint.constant = myTable.contentSize.height in viewDidAppear
How to increase height of text view when i type return key in key board?
please give me solution .
there are many views on top.
need i to set frame of all view or just decreasing height of table view i will get solution?
Well, every time you hit 'Enter' on keyboard, you have to resize (height) the UITextView, and also resize (height) the UITableView accordingly as well.
Try this code it will set the height of the TextView according to it's content.
[txtAddress setFrame:CGRectMake(20, 90, 280, [self contentSizeRectForTextView:txtAddress])];
Method for TextView height:
- (CGFloat)contentSizeRectForTextView:(UITextView *)txtView
{
[txtView.layoutManager ensureLayoutForTextContainer:txtView.textContainer];
CGRect textBounds = [txtView.layoutManager usedRectForTextContainer:txtView.textContainer];
CGFloat height = (CGFloat)ceil(textBounds.size.height + txtView.textContainerInset.top + txtView.textContainerInset.bottom);
return height;
}
When setting new frame size, your view frame origin should still be the same but the frame height.
[textView setFrame: CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textVie.frame.size.width,<new height>)];
In my UICollectionViewFlowLayout subclass I have this:
self.headerReferenceSize = CGSizeMake(280, 44);
However the header is displayed with a width of 320, which is the width of the collection view. This is correct according to the docs:
During layout, only the size that corresponds to the appropriate
scrolling direction is used. For example, for the vertical scrolling
direction, the layout object uses the height value returned by your
method. (In that instance, the width of the header would be set to the
width of the collection view.)
However, I need the width to be 280, and not stretch to the entire width of the collection view. How can I override this?
You can add another custom view over header and resize it as you wish. Ofcourse all you're components that were on header will go over custom view you created...
You don't need to add a subview to the header.
You want a header width of 280 in a Collection View of 320, so apply a Content Inset of 20 on the left and right, like so:
Swift
YourCollectionView.ContentInset = UIEdgeInsets(top: 0, left: 20, bottom: 20, right: 0)
Xamarin
YourCollectionView.ContentInset = new UIEdgeInsets(0, 20, 0, 20);
I have the following setup: a UIView containing a UILabel and a UIButton. The UIButton has fixed dimensions (it doesn't really matter). The UILabel, however, is constrained by the view's bounds.
I want to be able to set the UIView's width and it should automatically resize itself so that its height allows the whole label content to be visible.
I have tried the following:
self.view.frame = CGRectMake(0, 0, desiredWidth, 0);
CGSize fittingSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
self.view.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);
But this changes both the height and the width of the view, not keeping the width at the desired value.
Is there an elegant method to do this?
Thanks in advance for any help?
In AutoLayout, you would need add a width, top, bottom, leading (left) and/or trailing (right) constraints for the view on to the superview and declare properties for at least the top and/or bottom constraints. After adding the aforementioned constraints you could then change the height of the view by adjusting top and/or bottom constraints constant value.
Here is a link to the Auto Layout Guide.
Auto Layout Guide: Introduction