为什么在subView中添加UILabel会使其像素化

时间:2017-03-12 04:26:16

标签: ios swift uitableview uilabel

我正在制作一条消息,当tableView为空时显示该消息,并且我在子视图中添加了一个标签,并且它似乎是像素化的。但是当我在tableView中添加一些东西并将其删除时,显示的消息(UILabel)就完全没问题了。无法弄清楚原因。

在我的var b= JSON.stringify(JSON.parse(a));中添加标签解决了它,但我想添加两个标签,因此我在var b = [].concat(a);中添加一个标签,使其像素化。

这是我的代码:

self.tableView.backgroundView = emptyLabel

2 个答案:

答案 0 :(得分:1)

多次调用

class Animation {} // an analogue of the Animation.AnimationListener class interface AnimationListener { void onAnimationStart(Animation animation); void onAnimationEnd(Animation animation); } class AnimationListenerBuilder { // do nothing by default (avoiding a NPE from the build method) private Consumer<Animation> onAnimationStartCallback = animation -> {}; private Consumer<Animation> onAnimationEndCallback = animation -> {}; public AnimationListenerBuilder setOnAnimationStartCallback(Consumer<Animation> supplier) { onAnimationStartCallback = supplier; return this; } public AnimationListenerBuilder setOnAnimationEndCallback(Consumer<Animation> supplier) { onAnimationEndCallback = supplier; return this; } private AnimationListener build() { return new AnimationListener() { @Override public void onAnimationStart(Animation animation) { onAnimationStartCallback.accept(animation); } @Override public void onAnimationEnd(Animation animation) { onAnimationEndCallback.accept(animation); } }; } public static void main(String[] args) { AnimationListenerBuilder builder = new AnimationListenerBuilder(); AnimationListener listener = builder .setOnAnimationEndCallback(animation -> {}) .setOnAnimationEndCallback(animation -> {}) .build(); } } ,因此没有好的地方添加子视图,因为每次调用此函数时,另一个UILabel的实例都会添加到视图层次结构中。

删除numberOfRowsInSection中的添加标签。添加类变量:

numberOfRowsInSection

然后添加函数

var emptyLabel: UILabel!
var emptyLabel2: UILabel!
var emptyImage: UIImageView!

func createEmptyLabels() { emptyLabel=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: self.view.bounds.height)) emptyLabel2=UILabel(frame: CGRect(x: 0.0,y: 20.0, width: self.view.bounds.width, height: self.view.bounds.height)) self.view.addSubview(emptyLabel2) self.view.addSubview(emptyLabel) emptyImage = UIImageView(image: UIImage(named: "Quote Right Filled-100 (3)")) emptyImage.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(emptyImage) emptyImage.alpha=0.1 NSLayoutConstraint.activate([ emptyImage.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), emptyImage.centerYAnchor.constraint(equalTo: self.view.centerYAnchor), emptyImage.heightAnchor.constraint(equalToConstant: 90), emptyImage.widthAnchor.constraint(equalToConstant: 90) ]) emptyLabel.text = "no present reminders" emptyLabel.textColor=UIColor.darkGray emptyLabel.font=UIFont(name: "HelveticaNeue-Light", size: 18) emptyLabel.textAlignment = NSTextAlignment.center emptyLabel2.text = "you add by going back to the homescreen" emptyLabel2.textColor=UIColor.gray emptyLabel2.font=UIFont(name: "HelveticaNeue-Light", size: 11) emptyLabel2.textAlignment = NSTextAlignment.center emptyLabel.isHidden = true emptyLabel2.isHidden = true emptyImage.isHidden = true }

中调用此函数

viewDidLoad

numberOfRowsInSection

答案 1 :(得分:1)

您正在numberOfRowsInSection添加标签。问题是这种方法被多次调用。所以你要添加许多标签副本,堆叠在一起,这使得标签看起来很有趣。