I create a UITextView
with a UIToolbar
in code and add it to the View
of my MainViewController
use Autolayout
. But I find that when my MainViewController
is the first screen, the UITextView
does work well, if I put MainViewController
after a UITableViewController
, it is the second screen now, and it does not work well.
Screenshot second screen:
when I input the first t, UITextView
will scroll down.
when I input the second t, UITextView
will scroll up, show tt.
AppDelegate:
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Window = new UIWindow(UIScreen.MainScreen.Bounds);
// work well
//var navigation = new UINavigationController(new ChatViewController());
// not work well
var navigation = new UINavigationController(new MainViewController());
Window.RootViewController = navigation;
Window.MakeKeyAndVisible();
return true;
}
ChatViewController:
public class ChatViewController:UIViewController
{
public ChatViewController()
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// toolbar
var toolbar = new UIToolbar(){TranslatesAutoresizingMaskIntoConstraints = false};
toolbar.Layer.BorderColor = UIColor.LightGray.CGColor;
toolbar.Layer.BorderWidth = 1;
View.AddSubview(toolbar);
toolbar.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[toolbar(==42)]", 0, "toolbar", toolbar));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[toolbar]|", 0, "toolbar", toolbar));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[toolbar]|", 0, "toolbar", toolbar));
// inputTextView
var inputTextView = new UITextView(){ TranslatesAutoresizingMaskIntoConstraints = false };
inputTextView.Font = UIFont.SystemFontOfSize(16f);
inputTextView.Layer.BorderColor = UIColor.LightGray.CGColor;
inputTextView.Layer.BorderWidth = 1;
toolbar.AddSubview(inputTextView);
toolbar.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-[textview]-|", 0, "textview",inputTextView));
toolbar.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-[textview]-|", 0, "textview",inputTextView));
}
}
MainViewController:
public class MainViewController:UITableViewController
{
private List<string> strs;
public MainViewController()
{
strs = new List<string>
{
"one", "two", "three"
};
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return strs.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell("cellId");
cell = cell ?? new UITableViewCell(UITableViewCellStyle.Default, "cellId");
cell.TextLabel.Text = strs[indexPath.Row];
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var chatController = new ChatViewController();
chatController.View.BackgroundColor = UIColor.White;
NavigationController.PushViewController(chatController, true);
}
}
Update 1
Finally I find NavigationController.PushViewController
method cause the error, if I use PresentViewController
method, UITextView
recover normal. So how to fix this, what's wrong with PushViewController
, Autolayout
and UITextView
?
Finally I find the solution from here.
3 different ways:
Set EdgesForExtendedLayout = UIRectEdge.None
.
AutomaticallyAdjustsScrollViewInsets = false
.
Create a new view and add it to the View
before add my toolbar
.