我有一个
1- UIView
class AddNewsView : UIView
{
UIButton addButton;
Database db = new Database(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "NewsDb.sqlite"));
public AddNewsView()
{
//declaring button
addButton = new UIButton(UIButtonType.RoundedRect);
addButton.SetTitle("Add news", UIControlState.Normal);
addButton.SizeToFit();
addButton.TouchUpInside += addButtonClicked;
Add(addButton);
//adding constraints
TranslatesAutoresizingMaskIntoConstraints = false;
AddConstraint(NSLayoutConstraint.Create(
addButton,
NSLayoutAttribute.Top,
NSLayoutRelation.Equal,
this,
NSLayoutAttribute.Top,
1f, 5f
));
AddConstraint(NSLayoutConstraint.Create(
addButton,
NSLayoutAttribute.Width,
NSLayoutRelation.Equal,
this,
NSLayoutAttribute.Width,
0.5f, 0f
));
AddConstraint(NSLayoutConstraint.Create(
addButton,
NSLayoutAttribute.Height,
NSLayoutRelation.Equal,
this,
NSLayoutAttribute.Height,
0.5f, 0f
));
AddConstraint(NSLayoutConstraint.Create(
addButton,
NSLayoutAttribute.CenterX,
NSLayoutRelation.Equal,
this,
NSLayoutAttribute.CenterX,
1f, 0f
));
AddConstraint(NSLayoutConstraint.Create(
addButton,
NSLayoutAttribute.CenterY,
NSLayoutRelation.Equal,
this,
NSLayoutAttribute.CenterY,
1f, 0f
));
}
void addButtonClicked(object sender, EventArgs ea)
{
var news = new News();
news.Title = "News 1";
news.Text = " This is the text of news 1 ";
db.InsertNewsAsync(news);
new UIAlertView("Alert", "News added", null, "Dismiss", null).Show();
}
}
2- UIViewController
class NewsViewController : UIViewController
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "NewsDb.sqlite");
public override void ViewDidLoad()
{
base.ViewDidLoad();
//tableview under navbar
View.TranslatesAutoresizingMaskIntoConstraints = false;
EdgesForExtendedLayout = UIRectEdge.All;
AutomaticallyAdjustsScrollViewInsets = true;
ExtendedLayoutIncludesOpaqueBars = true;
//Adding news table
var table = new UITableView(View.Bounds);
table.Source = new TablesSources(new Database(path));
Add(table);
//Adding + button
UIBarButtonItem plusButton = new UIBarButtonItem(UIBarButtonSystemItem.Add);
plusButton.Clicked += addButtonClicked;
NavigationItem.RightBarButtonItem = plusButton;
}
void addButtonClicked(object sender , EventArgs ea)
{
NavigationController.PushViewController(new NewsAdditionViewController(), true);
}
}
3- UIViewController由前一个控制器推送
class NewsAdditionViewController : UIViewController
{
AddNewsView newsAdd;
public override void ViewDidLoad()
{
base.ViewDidLoad();
newsAdd = new AddNewsView();
View.BackgroundColor = UIColor.Black;
View.TranslatesAutoresizingMaskIntoConstraints = false;
EdgesForExtendedLayout = UIRectEdge.All;
AutomaticallyAdjustsScrollViewInsets = true;
ExtendedLayoutIncludesOpaqueBars = true;
//adding subview
newsAdd.BackgroundColor = UIColor.Orange;
View.AddSubview(newsAdd);
//setting constraints
View.AddConstraint(NSLayoutConstraint.Create(
newsAdd,
NSLayoutAttribute.Width,
NSLayoutRelation.Equal,
View,
NSLayoutAttribute.Width,
0.5f, 0f
));
View.AddConstraint(NSLayoutConstraint.Create(
newsAdd,
NSLayoutAttribute.Height,
NSLayoutRelation.Equal,
View,
NSLayoutAttribute.Height,
0.5f,0f
));
View.AddConstraint(NSLayoutConstraint.Create(
newsAdd,
NSLayoutAttribute.CenterX,
NSLayoutRelation.Equal,
View,
NSLayoutAttribute.CenterX,
1f, 0f
));
View.AddConstraint(NSLayoutConstraint.Create(
newsAdd,
NSLayoutAttribute.CenterY,
NSLayoutRelation.Equal,
View,
NSLayoutAttribute.CenterY,
1f, 0f
));
}
}
问题是 NewsAdditionViewController 视图没有占用屏幕,我希望它占据全屏,那么问题是什么呢?
答案 0 :(得分:0)
通过设置
修复 View.TranslatesAutoresizingMaskIntoConstraints = true;
在NewsAdditionViewController中