我正在用可可豆荚(Shuffle)构建一个应用程序,其中有一个cardStack(tinder风格),每个卡片在中心包含一个单词。我正试图用下面的代码为每张卡片添加一个按钮,但是当我设法以编程方式添加按钮时,我无法将其居中:当我使用“button.center=card.center”时,按钮将保留在“卡片视图”的坐标(0,0)处。我附上了一张截图,你可以看到右上角(0,0)坐标处的圆角白色按钮,如前所述。我错在哪里?它应该是相当初级的,但我被卡住了。
import UIKit
import Shuffle
class CardsViewController: UIViewController, SwipeCardStackDataSource, SwipeCardStackDelegate {
let cardStack = SwipeCardStack()
var actualIndex : Int = 0
var showingBack = false
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(cardStack)
cardStack.frame.size.height = view.frame.size.height - 180
cardStack.frame.size.width = view.frame.size.width
cardStack.center.y = CGFloat(cardStack.frame.size.height/2)
cardStack.center.x = CGFloat(cardStack.frame.size.width/2)
cardStack.center = CGPoint(x: view.center.x, y: view.center.y + 78)
cardStack.dataSource = self
// BUTTON
// NAVIGATION BAR TRANSPARENT
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
// CENTRAL BUTTON
}
func card(fromImage word: String) -> SwipeCard {
let card = SwipeCard()
card.swipeDirections = [.left, .right]
for direction in card.swipeDirections {
card.setOverlay(CardOverlay(direction: direction), forDirection: direction)
}
card.content = CardContentView(withWord: word)
card.footer = CardFooterView(withTitle: "", subtitle: "")
view.backgroundColor = .red
let button = UIButton()
button.frame.size = CGSize(width: 70, height: 70)
button.layer.cornerRadius = button.frame.width/2
button.clipsToBounds = true
button.backgroundColor = .init(white: 1, alpha: 1)
button.center = card.center
card.addSubview(button)
return card
}
func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard {
let theCard = card(fromImage: cardWords[actualIndex].word)
actualIndex = actualIndex + 1
return theCard
}
截图
问题是,当您试图添加按钮时,卡
框架尚未有效。
我还没有使用过这个“shuffle”pod,但是快速的查看告诉我你想要使用自动布局/约束,而不是尝试根据框架中心设置按钮的位置。
这样试试:
func card(fromImage word: String) -> SwipeCard {
let card = SwipeCard()
card.swipeDirections = [.left, .right]
for direction in card.swipeDirections {
card.setOverlay(CardOverlay(direction: direction), forDirection: direction)
}
card.content = CardContentView(withWord: word)
card.footer = CardFooterView(withTitle: "", subtitle: "")
view.backgroundColor = .red
// changes begin here...
let button = UIButton()
// use auto-layout / constraints
//button.frame.size = CGSize(width: 70, height: 70)
button.translatesAutoresizingMaskIntoConstraints = false
// add button to card
card.addSubview(button)
// button width = 70, height equalTo width
button.widthAnchor.constraint(equalToConstant: 70.0).isActive = true
button.heightAnchor.constraint(equalTo: button.widthAnchor).isActive = true
// center it in the card view
button.centerXAnchor.constraint(equalTo: card.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: card.centerYAnchor).isActive = true
// frame is not yet valid, so use 70 / 2
button.layer.cornerRadius = 35.0 //button.frame.width/2
button.clipsToBounds = true
button.backgroundColor = .init(white: 1, alpha: 1)
return card
}
根据您的SwipeCard
和CardContentView
类设置的方式,您可能需要将按钮添加到内容视图中,而不是添加到卡本身(您没有显示那些类,所以很难猜测)。
作为附带说明:您确实应该修改SwipeCard
类或CardContentView
类,并在其中添加按钮...