UITableView Auto Height With Constraint problems Xamarin.iOS - ios

I need to resize my UITableView with the height of his contentSize for this i made this:
private void ajustHeightOfTableView()
{
nfloat height = tableViewCores.ContentSize.Height;
nfloat maxHeight = tableViewCores.Superview.Frame.Size.Height - tableViewCores.Frame.Y;
if (height > maxHeight)
height = maxHeight;
UIView.Animate(0.25, new Action (() => {
this.tableViewAjustHeight.Constant = height;
this.contentView.SetNeedsUpdateConstraints();
}));
}
and
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
ajustHeightOfTableView();
}
i have a IBOutlet with de Height Constraint of my UITableView... so i resize the tableview calling after reloadData() my function ajustHeightOfTableView...
The problem is: Below my UITableView i have another UIView with buttons and textfields WHEN i resized my tableView my another View does not work! she does not respond for the user interaction... i thing that my problem is that after the resize the view is not in the correct position.
i have another constraint with the Vertical Spacing between the UITableView and UIView like this:
IMAGE 1
this constraint is to put my view in the correct space another my uitableview resize...
here my UITableView Constraint Height:
IMAGE2
someone please know what am i doing wrong here?

I don't know if you can adjust the height and width separately, but I know of a method to scale the webpage to your view in both directions. This is done using the ScalesPageToFit.
UIWebView webView = new UIWebView (new RectangleF (0, 0, cellWidth, cellHeight));
webView.ScalesPageToFit = true;
webView.ScrollView.MinimumZoomScale = 1f;
webView.ScrollView.MaximumZoomScale = 5f;
webView.LoadRequest (new NSUrlRequest(new NSUrl(URL)));
Adjusting the Frame of the UIWebView won't work.
I hope this short information helps. Good luck!
Love and regards,
Björn

Related

Dynamic height for UITableView ( Not Cell )

I'm trying to have a UITableView that lists all the different HomeKit devices a user has available.
Obviously there is no way to know how many devices they have, so I need to have the UITableView's height in the storyboard change.
I've tried this, which I call in the viewDidLoad() function:
func adjustHeightOfTableView() {
//getting the height of the tableview
var tableHeight = self.tableView.contentSize.height
//the height of the content inside the view
var maxHeight = self.tableView.superview?.frame.size.height
//if the height of the content is bigger then the height of the tableview
if (maxHeight! > tableHeight) {
tableHeight = maxHeight!
//set the tableview height to be the content height
}
//trying to reload the tableview height?
self.view.layoutIfNeeded()
self.tableView.reloadData()
}
I am trying to have some UI Elements under the tableview, and I want them to be a set space from the bottom of the tableview, but also have the tableview be the height that it needs to be, for whatever amount of cells there is.
But it's just not working.
If I'm doing anything wrong, or if anyone knows how to make this work, please let me know.
Thanks!
Note: For this approach you need to have static cell height or figure out a way to know before hand whats the total contentsize height
Assuming you are using constraints, create following constraints on your UITableView (apart from leading and trailing!)
Add a height constraint with a priority of 750 and a bottom spacing constraint of 0 to your super view that will be >= 0 and have a priority of 1000. Create outlet for this height constraint that you created in your UIViewController
Now,
func adjustHeightOfTableView() {
//set the height to be equal to the number of elements multiplied by the height of each cell.
//or use some logic that allows you to know what content size or space the cells will occupy!
tableViewHeightConstraint.constant = dataArray.count * rowHeight
view.layoutIfNeeded()
}
Now if your UITableView height is less than super view, no problems! But if it is greater than screen bounds, it will break the height constraint and become full screen and display the content normally as you expect a UITableView to!
Edit:
Even if you are using UIAutomaticRowDimensions what you can do is add constraints programmatically to your UITableView. i.e
Of course all your other views will still have a bottom constraint to your UITableView.
Create a UITableView in your storyboard with normal leading, trailing, top and bottom to the super view. Fetch the data. Get the contentSize for your UITableView and then remove the bottom constraint. Now add a height constraint that will be the minimum value of your UIScreen.main().bounds.size.height and contentSize.
you can use Automatic Dimensions if you are using autolayouts
in view didload:
let nib = UINib(nibName: "YOURCELLNIB", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "REUSEIDENTIFIER")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
Remove the function
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)
In your code, you have:
tableHeight = maxHeight!
//set the tableview height to be the content height
But this does not change the table height - it only changes some variable that previously was assigned the value of the old table content height. Nowhere in your code do you actually do anything to change the table height.
One way to change the table height directly is to assign it a completely new frame with values from the old frame, except for the frame's height, which you calculate however you like.
Try something like this (adding whatever other logic you need):
oldFrame = self.tableView.frame
newHeight = rowCount * rowHeight
self.tableView.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, oldFrame.size.width, newHeight)
There is a workaround which can make it seems like the height changes according to the number of the cells.
set tableview height to a proper value when init.
UITableView.init(frame: CGRect.init(x: 0, y: 70, width: self.view.frame.width, height: self.view.frame.height - 350))
set the tableview background color white transparent.
pulldownTableView?.backgroundColor = UIColor.white.withAlphaComponent(0)
set tableFooterView.
pulldownTableView?.tableFooterView = UIView(frame: CGRect.zero)
Below is the result, there are two table in the img. I set the transparent for the front tableview, left img set the backgroundColor to white, right white transparent.
----------------------vs----------------

The imageView in cells created by Xib will move a short distance when presented on screen

enter image description hereThis problem confuses me for a long time. I create cells by xib, and use autolayout to set its position. There is an imageView in the cell, I set the leading,trailing,top bottom of the imageView, There is no warnings on the constraints.
However, when the tableview shows on the screen, there is no problem on iPhone6, but strange move on iPhone5s and 6P. For 5s, the imageView shrink form 375 to 320; and for 6P, its width move from 375 to 414. All the moves happened in less than 1 second, but users can find it obviously.
I tried to change the width of xib from 375 to 320, and it comes out that the move disappeared on 5s,but showed on 6 and 6p.And when I change xib width from 375 to 414, the move shows on 5s and 6.
I don't know where the bug is, could anyone teach me, thank you so much!
Try to reload the table at the viewDidLayoutSubviews method of the view controller - reload it only if the width of the view of the view controller was changed.
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat width = self.view.frame.size.width;
CGFloat e = 0.01;
if (ABS(width - self.prevWidth) > e) {
self.prevWidth = width;
//reload table
}
}
or in swift:
var prevWidth: CGFloat = 0
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let width = self.view.frame.width
let e : CGFloat = 0.01
if abs(width - self.prevWidth) > e {
self.prevWidth = width
//reload the table
}
}
Set constraints like : leading,trailing,top,bottom to cell. it may solve your issue.

TableViewController Horizontal Scrollbar for iOS

I have a problem with uisplitviewcontroller. master controller is a tabbarviewcontroller and child controller is just a webview.. So I show some document on the rightside and leftside i want to show document information in tableview.
As you see on the figure (i draw red rectangles), some text are not fit on the cell, and i need to scroll it, but as a default, there is no horizontal scrolling in tableview. How to solve this problem?
I am using xamarin.ios but you can provide me obj-C or swift code or algorithm.
1 you can increase the tableView width
2 you can place UIScrollView on the cell and other labels move in that scrollView, then you can scroll the content of each cell
3 you can use UITextView in readonly mode instead of the second label in the cell, the scroll should be available automatically for the last text
I solved the problem.. Thank you Igor for the idea..
public CustomInformationCell (NSString cellId,int maxLenght,int totalCharLenght)
: base(UITableViewCellStyle.Default, cellId)
{
_maxLength = maxLenght; //max lenght is the longest label length in order to making alignment
SelectionStyle = UITableViewCellSelectionStyle.None;
Accessory = UITableViewCellAccessory.None;
ContentView.BackgroundColor = UIColor.White;
detail = new UILabel();
caption = new UILabel ();
caption.TextColor = UIColor.FromRGB (48,110,255);
_scrollView = new UIScrollView {
Frame = new CGRect (0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height),
ContentSize = new CGSize (ContentView.Bounds.Width + totalCharLenght, ContentView.Bounds.Height),
BackgroundColor = UIColor.White,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
_scrollView.AddSubview (caption);
_scrollView.AddSubview (detail);
ContentView.AddSubviews(new UIView[] {_scrollView});
//ContentView.AddSubviews(new UIView[] {caption, detail});
}

MvvmCross Resize UITableView once binded

I have a MvxTableViewSource which is bound to my TableView, I want the TableView's frame height to match it's content height. The below code doesn't do anything visual to my app even though the TableView's height has been updated (at breakpoint in ViewDidAppear), the TableView's height display to what was set initially in ViewDidLoad.
Any ideas of how I can programmatically set the frame height of the binded TableView so it shows on screen, the value seems to be set correctly but the UI is not being updated?
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
var frame = _jobTable.Frame;
frame.Height = _jobTable.ContentSize.Height;
frame.Width = View.Frame.Width;
_jobTable.Frame = frame;
_jobTable.SetNeedsDisplay();
}

Hide search bar in UICollectionView

I have added a search bar in a SectionHeader cell from the UICollectionView.
Currently I'm hiding the view by moving the Y-offset up.
[self.collectionView setContentOffset:CGPointMake(0, 44)];
This works perfectly when the height of my offset is bigger than my view. (vertical scrollbar)
But when the cells fit into my view, the search bar keeps still visible. (no vertical scrollbar)
Any idea?
Ty
What I did was subclass UICollectionViewFlowLayout and override the method:
- (CGSize)collectionViewContentSize {
CGSize size = [super collectionViewContentSize];
// add viewHeight to allow enough room for view to be hidden
if (size.height < self.collectionView.frame.size.height + viewHeight) {
size.height = self.collectionView.frame.size.height + viewHeight;
}
return size;
}
This does mean people can scroll a little bit on your collectionView when the size of the content is smaller than the bounds of your collectionView.
Sounds like you may just need to set alwaysBounceVertical:YES on your collectionView.

Resources