在堆栈视图中的某些元素内设置空间

时间:2016-10-17 01:48:15

标签: ios swift

基本上我正在尝试创建一个UIStackView,其中包含一些具有顶边距和底边距的元素。例如

| item one |  
| item two |  
|          |  
| itemfour | 

我试图添加spacerView:

let textField1 = UITextField(text: "one")
let textField2 = UITextField(text: "two")
let textField3 = UITextField(text: "four")
let spacerView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))

viewStack.addSubView(textField1)
viewStack.addSubView(textField2)
viewStack.addSubView(spacerView)
viewStack.addSubView(textField3)

但是,我设置spacerView的高度似乎不会影响堆栈视图中创建的空间。

PS:如果有更好的方法在两个项目之间添加顶部和底部边距,我绝对愿意接受,因为添加UIView感觉非常hacky

1 个答案:

答案 0 :(得分:0)

你可以:

  1. 制作一个UILabel text " "一个空格(fillEqually)的间隔视图。这将为您提供一个空白行,其高度与文本的其余部分相同。如果分发设置为distribution(它也适用于其他模式,但我之所以提到它,因为UIView在下面的选项#2中很重要),这将有效。
  2. ,或者

    1. 创建intrinsicContentSize的子类并覆盖其CGSize,返回指定所需高度的frame。不要设置此视图UIStackView(即让UIStackView管理视图的框架)。请注意,此方法仅在fillEqually的分布设置为/** * Created by foolishklown on 10/13/2016. */ var assert = require('assert'), path = require('path'), Grid = require('gridfs-stream'), fs = require('fs'), mongodb = require('mongodb'), mongoose = require('mongoose'), User = require('../../models/user'), mediaUri = 'mongodb://localhost/media', userUri = 'mongodb://localhost/mean-auth'; module.exports = { writeFile: function (file, userId, fileType, fileInfo, res) { var fileId; var fileTitle = file.originalFilename; var conn = mongoose.createConnection('mongodb://localhost/media', (error) => { if (error) { console.error('Error connecting to mongod media instance'.red); process.exit(1); } else { console.info('Connected successfully to mongod media instance in the write file!'.blue); } }); // The following line is designating a file to grab/read, and save into mongo // in our case it will be something from ng-file-upload that the user wants to upload var myFile = file.path; // Connect gridFs and mongo Grid.mongo = mongoose.mongo; conn.once('open', function () { console.log('connection open, ready for I/O!'); var gfs = Grid(conn.db); // This write stream is how well write to mongo var writeStream = gfs.createWriteStream({ // Name the file the way you want it stored in mongo filename: file.originalFilename, type: fileType, info: fileInfo }); // Create a read stream, so that we can read its data, and then with that well use the // write stream to write to the DB via piping the writestream var readStream = fs.createReadStream(myFile) .on('end', () => { writeToUserDb(userId, fileType, readStream.id, fileInfo, fileTitle); res.status(200).send( { ref: readStream.id, type: fileType, user: userId, mediaInfo: fileInfo, title: fileTitle } ); }) .on('error', () => { res.status(500).send('error in writing with gridfs'); }) .pipe(writeStream); writeStream.on('close', function (file) { console.log(file.filename + 'written to DB'); fs.unlink(myFile); myFile = null; conn.close(); }); }); function writeToUserDb(uid, type, fileId, authInfo, title) { console.info('called to write to user db without a \'this\' reference'); var userConn = mongoose.createConnection('mongodb://localhost/mean-auth', (error) => { if (error) { console.error('Error connecting to the mean-auth instance'.red); process.exit(1); } else { User.findById(uid, (err, doc) => { if (err) { console.error('Error finding user with id: ', uid); process.exit(1); } else { console.log('original doc: ', doc); doc.addMedia(type, fileId, authInfo, title); doc.save(); console.log('new doc: ', doc); } }) } }); userConn.close(); } }, downloadFile: function (userId, file, fileType, objId, location, res) { console.info('called to download file'); var id = new mongodb.ObjectID(objId); var conn = mongoose.createConnection(mediaUri, (error) => { assert.ifError(error); var gfs = Grid(conn.db, mongoose.mongo); gfs.findOne({_id: id}, (err, result) => { if (err) { res.status(400).send(err); } else if (!result) { res.status(404).send('Error finding file') } else { res.set('Content-Type', result.contentType); res.set('Content-Disposition', 'attachment; filename="' + result.filename + '"'); var readStream = gfs.createReadStream({ _id: id, root: 'resume' }); readStream.on('error', (err) => { res.end(); }); readStream.pipe(res); } }); }); conn.close(); }, deleteFile: function (userId, fileType, objId, res) { var client = mongodb.MongoClient; var id = new mongodb.ObjectID(objId); console.log('object id to find is: ', id); client.connect('mongodb://localhost/media', (err, db) => { db.collection('fs.files', {}, (err, files) => { files.remove({_id: id}, (err, result) => { if (err) { console.log(err); res.status(500); } console.log(result); }); }); db.collection('fs.chunks', {}, (err, chunks) => { chunks.removeMany({files_id: id}, (err, result) => { if (err) { console.log(err); res.status(500); } console.log(result); }); }); db.close(); }); res.status(200).send({id: userId, type: fileType, ref: id}); }, getAll: function (req, res) { var uid = req.query.id; var conn = mongoose.createConnection('mongodb://localhost/mean-auth', (err) => { if (err) { console.error('Error connecting to mean-auth instance to read all'); process.exit(1); } else { User.findById(uid, (err, doc) => { if (err) { console.error('Error finding user with id: ', uid); process.exit(1); } else { if (doc) { console.log('original doc: ', doc); res.status(200).send({media: doc.media}); } else { res.status(200); } } }) } }); } }; 时才有效。