我试图将锚约束放到subView中包含的textField上。对于前锚和后锚,它们不起作用。我不确定这是什么,我想在键盘和子视图中的项之间留一些空间,以及在UITextField和屏幕的尾随锚之间留一些空间。下面是有问题的代码
布局代码:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Platform } from 'ionic-angular';
import { SQLite, SQLiteObject} from '@ionic-native/sqlite';
import { SQLitePorter } from '@ionic-native/sqlite-porter'
import { BehaviorSubject } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
/*
Generated class for the DatabaseProvider provider.
See https://angular.io/guide/dependency-injection for more info on
providers
and Angular DI.
*/
@Injectable()
export class DatabaseProvider {
database: SQLiteObject;
private dbReady: BehaviorSubject<boolean>;
constructor(public http: HttpClient, private sqlitePorter: SQLitePorter, private storage: Storage, private sqlite: SQLite, private platform: Platform)
{
this.dbReady = new BehaviorSubject(false);
this.platform.ready().then(() => {
this.sqlite.create({
name: 'assessment.db',
location: 'default'
})
.then((db:SQLiteObject) => {
this.database = db;
this.storage.get('database_filled').then(val => {
if(val){
this.dbReady.next(true);
}
else{
this.fillDatabase();
}
})
});
});
}
fillDatabase(){
this.http.get('assets/test.sql')
.map (res => res.text())
.subscribe(sql => {
this.sqlitePorter.importSqlToDb(this.database, sql)
.then(data => {
this.dbReady.next(true);
this.storage.set('database_filled', true);
})
});
}
getDatabaseState(){
return this.dbReady.asObservable();
}
}
完整的代码
func setUpLayout(){
//myView
self.myView.translatesAutoresizingMaskIntoConstraints = false
self.myView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
self.myView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
self.myView.heightAnchor.constraint(equalToConstant: 98).isActive = true
self.myView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 4).isActive = true
//CollectionView
self.collectionView.translatesAutoresizingMaskIntoConstraints = false
self.collectionView.leadingAnchor.constraint(equalTo: self.myView.leadingAnchor, constant: 0).isActive = true
self.collectionView.trailingAnchor.constraint(equalTo: self.myView.trailingAnchor, constant: 0).isActive = true
self.collectionView.bottomAnchor.constraint(equalTo: self.myView.bottomAnchor, constant: 4).isActive = true
self.collectionView.topAnchor.constraint(equalTo: self.searchBar.bottomAnchor, constant: 4).isActive = true
//searchbar
self.searchBar.translatesAutoresizingMaskIntoConstraints = false
self.searchBar.topAnchor.constraint(equalTo: self.myView.topAnchor, constant: 0).isActive = true
self.searchBar.leadingAnchor.constraint(equalTo: self.myView.leadingAnchor, constant: 4).isActive = true
self.searchBar.trailingAnchor.constraint(equalTo: self.myView.trailingAnchor, constant: 4).isActive = true
self.searchBar.heightAnchor.constraint(equalToConstant: 45).isActive = true
searchBar.backgroundColor = .white
searchBar.layer.borderWidth = 2
searchBar.layer.borderColor = UIColor.black.cgColor
//PictureView
self.PictureView.translatesAutoresizingMaskIntoConstraints = false
self.PictureView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
self.PictureView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
self.PictureView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
self.PictureView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
self.PictureView.backgroundColor = .green
}
答案 0 :(得分:0)
对于尾部和底部锚点约束,通常必须对常量使用负数才能实现所需的效果。
例如:
searchBar.bottomAnchor.constraint(equalTo: myView.bottomAnchor, constant: 4).isActive = true
成为:
searchBar.bottomAnchor.constraint(equalTo: myView.bottomAnchor, constant: -4).isActive = true