EventWebView.frame.size = EventWebView.sizeThatFits(CGSize.zero)
EventWebView.scrollView.isScrollEnabled = false;
var frame = EventWebView.frame;
frame.size.width = EventTableView.frame.size.width-40; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
EventWebView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = EventWebView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
heightConstraint.constant = frame.size.height
My HTML Content is "
Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.\n"
Add WebViewDelegate
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size = webView.sizeThatFits(CGSize.zero)
webView.scrollView.isScrollEnabled = false; // Property available in iOS 5.0 and later
var frame = webView.frame;
frame.size.width = self.view.frame.width-20; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
webView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = webView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
webView.frame = frame;
fl = webView.frame.size.height
}
Make sure please check you autolayout constraint also. This method working fine. if your auto layout are not set properly then you can get extra length in it.
Try this
NSString *contentHeight = [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
int height = [contentHeight integerValue];
in your webViewDidFinishLoad method. Make sure you set delegate for web view and implement the above delegate methods in your view controller.
Related
How do I render a UIButton in Xamarin.iOS? See the current Code for the full list.
This is the code I'm using to create and add the button to the Xamarin.Forms.Platform.iOS.CellTableViewCell cell. I cannot get the button to display anything.
With the use of a Foundation.NSMutableAttributedString, it shows a cut-off section of text in the top left corner, regardless of anything I try (alignments, insets, bounds, various constraints, etc). I'm currently trying things from Xamarin.Forms.Platform.iOS.Renderers.ButtonRenderer, but still can't get anything to display at all, no text, no button, or its outline.
If you could fork the repo and fix it or post the solution here, I would be very grateful.
protected override void SetUpContentView()
{
var insets = new UIEdgeInsets(SVConstants.Cell.PADDING.Top.ToNFloat(), SVConstants.Cell.PADDING.Left.ToNFloat(), SVConstants.Cell.PADDING.Bottom.ToNFloat(), SVConstants.Cell.PADDING.Right.ToNFloat());
_Button = new UIButton(UIButtonType.RoundedRect)
{
AutoresizingMask = UIViewAutoresizing.All,
HorizontalAlignment = UIControlContentHorizontalAlignment.Center,
VerticalAlignment = UIControlContentVerticalAlignment.Center,
ContentEdgeInsets = insets,
// TitleEdgeInsets = insets
};
DefaultFontSize = _Button.TitleLabel.ContentScaleFactor;
DefaultTextColor = _Button.TitleLabel.TextColor;
_Recognizer = new UILongPressGestureRecognizer(RunLong);
_Button.TouchUpInside += OnClick; // https://stackoverflow.com/a/51593238/9530917
_Button.AddGestureRecognizer(_Recognizer); // https://stackoverflow.com/a/6179591/9530917
ContentView.AddSubview(_Button);
_Button.CenterXAnchor.ConstraintEqualTo(ContentView.CenterXAnchor).Active = true;
_Button.CenterYAnchor.ConstraintEqualTo(ContentView.CenterYAnchor).Active = true;
_Button.WidthAnchor.ConstraintEqualTo(ContentView.WidthAnchor).Active = true;
_Button.HeightAnchor.ConstraintEqualTo(ContentView.HeightAnchor).Active = true;
UpdateConstraintsIfNeeded();
LayoutIfNeeded();
}
Found out that you can't subclass it. Any button added to the view must be native (UIButton) or custom rendered, such as Xamarin.Forms.Platform.iOS.ButtonRenderer; It doesn't show up otherwise.
I ported a table view implementation from Swift with a variable height cell. But in my Xamarin/ReactiveUI implementation, the cell does not resize when the content grows (i.e. when an optional label is displayed). Please note that the cell does resize to fit in the native Swift implementation.
Most answers online focus on the setting of two properties on UITableView: RowHeight and EstimatedHeight as well as using AutoLayout. My row height is set to UITableView.AutomaticDimension and the EstimatedHeight is set to 44f. I am using autolayout constraints which I will show below. I am using the BindTo extensions in ReactiveTableViewSourceExtensions.
I also tried setting the Text property of the optional label right away to test the theory that setting the property up front would provoke a resize.
Here are the pertinent lines of code:
In a ReactiveViewController<T> ctor:
this.WhenActivated(disposables =>
{
//// ...
// This method automatically wires up the cell reuse key to be the nameof my cell class which is what I want.
this.WhenAnyValue(view => view._cells)
.BindTo<IImportedFileViewModel, ImportedFileCell>(TableView, 44f)
.DisposeWith(disposables);
//// ...
});
Below in ViewDidLoad:
//// ...
TableView = new UITableView
{
RowHeight = UITableView.AutomaticDimension,
EstimatedRowHeight = 44f, // Setting or not setting this doesn't matter
SeparatorStyle = UITableViewCellSeparatorStyle.None,
AllowsSelection = false,
TranslatesAutoresizingMaskIntoConstraints = false,
};
//// ...
In the cell implementation:
ClipsToBounds = true;
PreservesSuperviewLayoutMargins = true;
ContentView.ClipsToBounds = true;
ContentView.PreservesSuperviewLayoutMargins = true;
var stackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Vertical,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Fill,
Spacing = 4,
};
var fileStackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Top,
};
FilenameLabel = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Font = UIFont.SystemFontOfSize(15f, UIFontWeight.Medium),
};
fileStackView.AddArrangedSubview(FilenameLabel);
StatusImage = new UIImageView()
{
TranslatesAutoresizingMaskIntoConstraints = false,
ContentMode = UIViewContentMode.Center,
};
fileStackView.AddArrangedSubview(StatusImage);
stackView.AddArrangedSubview(fileStackView);
var reasonStackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Top,
};
// This is the optional label that, when its Text property is set, should resize the cell.
FailureReasonLabel = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Font = UIFont.SystemFontOfSize(13f, UIFontWeight.Medium),
Lines = 0,
};
reasonStackView.AddArrangedSubview(FailureReasonLabel);
stackView.AddArrangedSubview(reasonStackView);
ContentView.AddSubview(stackView);
stackView.BottomAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.BottomAnchor).Active = true;
stackView.TopAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.TopAnchor).Active = true;
stackView.LeadingAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.LeadingAnchor).Active = true;
stackView.TrailingAnchor.ConstraintEqualTo(ContentView.LayoutMarginsGuide.TrailingAnchor).Active = true;
StatusImage.HeightAnchor.ConstraintEqualTo(16f).Active = true;
StatusImage.WidthAnchor.ConstraintEqualTo(16f).Active = true;
I would be happy to get a solution to the problem using the basic structure I have now but I would also accept an example of some other pattern that someone has used (based on ReactiveUI) to get this working. I would prefer that the solution not be based on old manual resizing patterns like in the pre-iOS 8 days or some sort of hack.
So two things had to be adjusted to fix my issue.
The sizeHint parameter in the ReactiveUI BindTo method does not behave like TableView.EstimatedRowHeight, which is what I was assuming. So I ended up setting it to the auto dimension constant like this:
.BindTo<IImportedFileViewModel, ImportedFileCell>(TableView, (float)UITableView.AutomaticDimension)
I was thinking that I could just update the properties of the view models within the IObservableCollection implementation that is passed in to the BindTo method but it wasn't until I started provoking changed events on the collection itself (the collection that the view is bound to) that the cells started resizing themselves. So since I am using DynamicData as part of ReactiveUI, that meant calling SourceCache.AddOrUpdate(updatedViewModel) whenever I knew that the FailureReasonLabel had been set.
I am also going to try and restore the ReactiveUI tag that was removed from this post because I believe it to be a relevant part of this question and its answer.
I want to change UITableView height depending upon user interaction with a button. When user clicks on the button, UITableView gets expanded and click on the same button UITableView height is decreased only to show 1st row. Following is my code
tblFaults?.setNeedsUpdateConstraints()
self.view.setNeedsUpdateConstraints()
if (currentNumber == 1)
{
currentNumber = 2
var newFrame = tblFaults?.frame
newFrame?.size.height += 67;//tried to change frame as well
tblFaults?.frame = newFrame!
faultViewHeight?.constant = 115
//faultView height is tableview height constraint
}
else
{
currentNumber = 1
var newFrame = tblFaults?.frame
newFrame?.size.height -= 67;
tblFaults?.frame = newFrame!
faultViewHeight?.constant = 48
}
tblFaults?.reloadData()
//self.view.setNeedsLayout()
//self.view.setNeedsDisplay()
I tried setNeedsDisplay, setNeedsLayout as well as layoutSubviews() after the code. But view does not get refreshed immediately after button click but gets refreshed only after next user interaction (e.g. scrollview change etc)
Try this :
faultViewHeight?.constant = 48
tblFaults.layoutIfNeeded() // you need to call this method
Thanks everyone.
Actually only constraint constant value change and tableview.reloadData() was enough. The BIG mistake I did was, I was checking the execution on simulator. I tried this on device and it worked perfectly fine. On simulator I was facing the issue even after trying self.view.layoutIfNeeded(). I still do not know why this is the behaviour on simulator and not on device. But eventually app has to run on device and its working there. :)
Thank you all.
What I have is a UILabel (loaded with html) that I put on a UIScrollView.
In the html I have lots of links that I can´t get to "fire-up" when clicked.
I have added UserInteractionEnabled = true both on the UILabel and on the UIScrollview but I can´t get any reaction from the taps on the hyperlinks.
This is the code that create the label with the content.
var attr = new NSAttributedStringDocumentAttributes();
var nsError = new NSError();
attr.DocumentType = NSDocumentType.HTML;
var entry = "some text and url <a href='http://www.google.com'>url</a>";
var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);
contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width , scrollView.Frame.Size.Height);
contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
contentLabel.Lines = 0;
contentLabel.SizeToFit();
Maybe this is just wrong.. putting html content into a label? But I don´t want to use a webview since it does not seem to scale that well.
Why not using a UITextView instead ? Enable Links detection and disable editable and Selectable option.
Adding HTML to UILabel will not make it interactive.
Use UITextView or https://github.com/TTTAttributedLabel/TTTAttributedLabel
I'm new to iOS and autolayout. I'd like to implement a simple UI like iMessage. The structure is simple like,
---------------
TableView (for history content)
---------------
charBarView ( contains inputTextView | sendButton )
---------------
The constraints for inputTextView is TopSpace/BottomSpace/Leading with charBarView, and Trailing with sendButton. Also I set its height equals 45, while charBarView height unset.
The problem is, when I try to handle user input more than one line, to implement the delegate textViewDidChange. The code :
func textViewDidChange(textView: UITextView) {
if textView != inputTextView{
return
}
var textViewFrame = inputTextView.frame
let sizeDiff = inputTextView.contentSize.height - textViewFrame.height
var chatBarViewFrame = chatBarView.frame
chatBarViewFrame.origin.y -= sizeDiff
//chatBarViewFrame.size.height += sizeDiff
chatBarView.frame = chatBarViewFrame
inputTextViewFrame.size = inputTextView.contentSize
inputTextView.frame = inputTextViewFrame
var tableViewFrame = tableView.frame
tableViewFrame.size.height -= sizeDiff
tableView.frame = tableViewFrame
}
When I comment the code
//chatBarViewFrame.size.height += sizeDiff
it looks work fine, but the height of chatBarView is unchanged, so the button is moved up when charBarView changed(I want to keep it at bottom always).
When I put the code
chatBarViewFrame.size.height += sizeDiff
back, then the charBarView origin and height is changed, but the height of inputTextView will never change.
I use xcode 7 with iOS 8/9.
Thanks for any help!