프로그래밍 방식으로 스택 뷰를 설정하려고합니다. 나는 모든 설정을 바르게해야한다고 확신한다. 그러나 stack view back ground와 같은 단순한 것들을 조정하려고 할 때나 중앙에 맞춰서 정렬 할 때 아무것도 바뀌지 않고 그 모두가 왼쪽 상단에 정렬된다.프로그래밍 방식으로 스택 뷰를 변경하지 않음
다음은 코드입니다.
ShowGuideViewController :
import UIKit
import OAStackView
class ShowGuideViewController: UIViewController {
let stackView = OAStackView()
override func viewDidLoad() {
super.viewDidLoad()
ShowGuideItem.all { showGuideItems in
showGuideItems.forEach(self.viewForShowGuideItem)
}
stackView.axis = UILayoutConstraintAxis.Vertical
stackView.distribution = OAStackViewDistribution.FillEqually
stackView.alignment = OAStackViewAlignment.Center
stackView.backgroundColor = UIColor.blueColor()
view.addSubview(stackView)
}
private func viewForShowGuideItem(showGuideItem: ShowGuideItem) {
let view = ShowGuideItemView(frame: CGRectZero)
view.bind(showGuideItem)
view.tapped = { _ in
print("Open \(showGuideItem.url)")
}
stackView.addArrangedSubview(view)
}
}
표시 가이드 항목 :
import Foundation
import Freddy
struct ShowGuideItem {
let name: String
let text: String
let iconFileName: String
let backgroundImageFileName: String
let url: String
}
extension ShowGuideItem: JSONDecodable {
init(json: JSON) throws {
name = try json.string("name")
text = try json.string("text")
iconFileName = try json.string("iconFileName")
backgroundImageFileName = try json.string("backgroundImageFileName")
url = try json.string("url")
}
}
extension ShowGuideItem {
static func all(completion: ([ShowGuideItem]) ->()) {
let carsData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("ShowGuide", withExtension: "json")!)!
do {
let json = try JSON(data: carsData)
let items: [ShowGuideItem] = try json.arrayOf("ShowGuideItems")
completion(items)
} catch {
fatalError("Failed to load Show Guide Items")
}
}
}
전시회 안내 항목보기 :
import UIKit
class ShowGuideItemView: UIView {
var nameLabel: UILabel!
var textBody: UITextView!
var iconImageView: UIImageView!
var backGroundImageView: UIImageView!
var tapped: (ShowGuideItemView) ->() = { _ in }
private var touched = false
override init(frame: CGRect) {
super.init(frame: frame)
setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func bind(item: ShowGuideItem) {
nameLabel.text = item.name
textBody.text = item.text
iconImageView.image = UIImage(named: item.iconFileName)
backGroundImageView.image = UIImage(named: item.backgroundImageFileName)
}
internal override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touched = true
}
internal override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if touched {
touched = false
tapped(self)
}
}
internal override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
touched = false
}
private func setupSubviews() {
nameLabel = UILabel(frame: CGRect.zero)
textBody = UITextView(frame: CGRect.zero)
iconImageView = UIImageView(frame: CGRect.zero)
backGroundImageView = UIImageView(frame: CGRect.zero)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
textBody.translatesAutoresizingMaskIntoConstraints = false
iconImageView.translatesAutoresizingMaskIntoConstraints = false
backGroundImageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(nameLabel)
addSubview(textBody)
addSubview(iconImageView)
addSubview(backGroundImageView)
}
}
당신이 모든 것을 내가 가지고 JSON 파일에서 읽어 볼 수있다 로깅 된 작품과 출력을 정확하게 볼 때 모든 건물 자체를 겹쳐 볼 수 있습니다.
stackView.axis = UILayoutConstraintAxis.Vertical
stackView.distribution = OAStackViewDistribution.FillEqually
stackView.alignment = OAStackViewAlignment.Center
stackView.backgroundColor = UIColor.blueColor()
: 나는 같은 라인을 추가 할 때, 그래서 등
자체를 stackview을 중심으로 수직으로 정렬로 시작하고 싶어 나는이를 havent
아직 stackview의 각 항목을 포맷 할 수있어 viewDidLoad에 아무것도 변화하고 있지 않으며 나는 그것이 왜 wouldnt인지 볼 수 없다.
를? –