有什么想法吗?
import UIKit
import PlaygroundSupport
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
let v0 = UIView(frame: CGRect(0, 0, 500, 500))
v0.backgroundColor = .white
let v1 = UIView(frame: CGRect(0,0,500,100))
v1.backgroundColor = .red
v1.translatesAutoresizingMaskIntoConstraints = false
let v2 = UIView(frame: CGRect(0,200,500,100))
v2.backgroundColor = .yellow
v2.translatesAutoresizingMaskIntoConstraints = false
let v3 = UIView(frame: CGRect(0,400,500,100))
v3.backgroundColor = .blue
v3.translatesAutoresizingMaskIntoConstraints = false
let sv = UIStackView(frame: CGRect(0, 0, 500, 500))
sv.addArrangedSubview(v1)
sv.addArrangedSubview(v2)
sv.addArrangedSubview(v3)
sv.axis = .vertical
sv.alignment = .center
sv.distribution = .fillEqually
sv.translatesAutoresizingMaskIntoConstraints = false
sv.backgroundColor = .orange
v0.addSubview(sv)
NSLayoutConstraint.activate([
sv.topAnchor.constraint(equalTo: v0.topAnchor),
sv.leadingAnchor.constraint(equalTo: v0.leadingAnchor),
sv.trailingAnchor.constraint(equalTo: v0.trailingAnchor),
sv.bottomAnchor.constraint(equalTo: v0.bottomAnchor),
])
PlaygroundPage.current.liveView = v0
这就是我所看到的......
答案 0 :(得分:0)
在相反的轴上,它需要对大小进行约束。
因此,当stackview处于垂直模式时,我添加了这些约束......
let parameters = ["part":"snippet","maxResults":50,"channelId":CHANNEL_ID,"playlistId":UPLOADS_PLAYLIST_ID,"key":API_KEY] as [String : Any]
class VideoModel: NSObject {
var videoArray = [Video]()
func getFeedVideo() {
Alamofire.request("https://www.googleapis.com/youtube/v3/playlistItems", parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if let JSON = response.result.value {
if let dictionary = JSON as? [String: Any] {
var arrayOfVideos = [Video]()
guard let items = dictionary["items"] as? NSArray else { return }
for items in dictionary["items"] as! NSArray {
print(items)
// Create video objects off of the JSON response
let videoObj = Video()
videoObj.videoID = (items as AnyObject).value(forKeyPath: "snippet.resourceId.videoId") as! String
videoObj.videoTitle = (items as AnyObject).value(forKeyPath: "snippet.title") as! String
videoObj.videoDescription = (items as AnyObject).value(forKeyPath: "snippet.description") as! String
videoObj.videoThumbnailUrl = (items as AnyObject).value(forKeyPath: "snippet.thumbnails.maxres.url") as! String
arrayOfVideos.append(videoObj)
}
self.videoArray = arrayOfVideos
if self.delegate != nil {
self.delegate!.dataReady()
}
}
}
}
}
横向......
v1.widthAnchor.constraint(equalTo: v0.widthAnchor)
v2.widthAnchor.constraint(equalTo: v0.widthAnchor)
v3.widthAnchor.constraint(equalTo: v0.widthAnchor)
还可以删除彩色视图上的帧。