我正在尝试创建一个自定义视图,该视图由UIVabel顶部的UILabel组成。这个想法是一个小的弹出消息,标签与视图的大小基本相同。但是,每当我尝试将此自定义视图(由屏幕截图中屏幕底部的灰色小框表示)添加到另一个视图时,我只能显示UIView /深灰色背景,而不是标签。有人可以指出我正确的方向,因为我有点困惑,为什么下面的代码不起作用?感谢。
编辑:我应该指出,如果我用CGRect替换.zero(x:0,y:0,高度:100,宽度:50),我可以以某种形式显示标签,但我会有考虑到约束,将它设置为.zero会不会很好?
import UIKit
class PopUp: UIView {
var label: UILabel!
var labelText: String!
init(frame: CGRect, text: String) {
super.init(frame: frame)
self.labelText = text
self.backgroundColor = .darkGray
self.label = UILabel(frame: .zero)
self.label.textColor = UIColor.white
self.label.text = self.labelText
self.label.font = UIFont(name: "Times New Roman", size: 14.0)
self.translatesAutoresizingMaskIntoConstraints = false // this should be "self.label.translatesAutoresizingMaskIntoConstraints"
self.addSubview(self.label)
self.bringSubview(toFront: self.label)
NSLayoutConstraint.activate([
self.label.topAnchor.constraint(equalTo: self.topAnchor),
self.label.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.label.trailingAnchor.constraint(equalTo: self.trailingAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
答案 0 :(得分:0)
如果要将消息显示为弹出窗口,为什么不在UIViewController扩展中编写弹出窗口并在控制器中调用它?
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.affix {
position: fixed;
top: 0;
width: 100%;
z-index: 9999 !important;
background-color: #fff;
align-content: flex-end;
left: 0;
right: 0;
display: table;
margin: 0 auto;
max-width: calc(1170px); //container width - 30px padding
word-wrap: break-word;
}
.lin
{
color:windowtext;
font-size:8.0pt;
font-style:normal;
text-decoration:none;
font-family:Arial, sans-serif;
text-align:left;
vertical-align:bottom;
/*width:20px;*/
}
.sol
{
color:windowtext;
font-size:8.0pt;
font-style:normal;
text-decoration:none;
font-family:Arial, sans-serif;
text-align:left;
vertical-align:bottom;
/*width:20px;*/
}
</style>
</head>
<body>
<div class="container">
<h2>sssssssssdhhdhdhd dddddd for Hello sssssssdd</h2>
<table class="table table-striped" width:1170px>
<thead data-spy="affix" data-offset-top="10">
<tr>
<th>Current Rel<br/>
Arssstecture<br/>
fitrtyr<br/>
of OS
</th>
<th>aupporz <br/>
asdins</th>
<th>aussorz <br/>
znds</th>
<th><br/>
a34<br/>
(IAas)<br/>
31
</th>
<th>Pinux<br/>
a86_64<br/>
63
</th>
<th><br/>
<br/>
xCC
</th>
<th><br/>
<br/>
abc
</th>
<th><br/>
sd51<br/>
(sdanium)<br/>
64
</th>
<th>
sdhetr<br/><br/>
32
</th>
<th>
sdhetr<br/><br/>
64
</th>
<th>
pun/aassssu<br/>
aaaaa<br/>
64<br/>
</th>
<th>
pun<br/>
a86_64<br/>
14<br/>
</th>
<th>
cIX<br/>
weakiPC<br/>
63<br/>
</th>
<th>
aP<br/>
aA <br/>
aISC<br/>
34<br/>
</th>
<th>
pandows<br/>
a16<br/>
31<br/>
</th>
</tr>
</thead>
<tbody style="font-size:11px">
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
<tr>
<td>asd 16.6 (9)</td>
<td >10/24/1800</td>
<td >3/31/1900</td>
<td >NA</td>
<td class = "lin">asdf5, asdf6, ssss 10, ssss 11</td>
<td ></td>
<td ></td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sdhetr 10 (34 dftOnly)</td>
<td >NA</td>
<td >NA</td>
<td >NA</td>
<td class = "sol">sd oiu, sdhetr 2008, Vist enterenter, winwins 7 (32/64)
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
并在视图控制器中调用该函数:
extension UIViewController {
func showPopup(text: String) {
let popupView: UIView = {
let view = UIView()
view.backgroundColor = .darkGray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let textLabel: UILabel = {
let label = UILabel()
label.text = self.text
label.font = UIFont(name: "Times New Roman", size: 14.0)
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
self.view.addSubview(popupView)
// add anchors to your popupView relative to your viewController here
self.popupView.addSubView(textLabel)
self.textLabel.topAnchor.constraint(equalTo: popupView.topAnchor).isActive = true
self.textLabel.bottomAnchor.constraint(equalTo: popupView.bottomAnchor).isActive = true
self.textLabel.leftAnchor.constraint(equalTo: popupView.leftAnchor).isActive = true
self.textLabel.rightAnchor.constraint(equalTo: popupView.rightAnchor).isActive = true
}
}
答案 1 :(得分:0)
初始化后将文本设置为标签
self.label = UILabel(frame: .zero)
self.labelText = text
您在初始化之前设置文本。
答案 2 :(得分:0)
我对你的剧本进行了一些测试,从头开始重写,并抓住了你的错误。
问题是,你没有导入字体&#34; Times New Roman&#34;。为此,您需要右键单击黄色文件夹,按Command + Shift + G,然后在以下位置键入:
/System/Library/Fonts/
然后,向下滚动,直到看到显示&#34; Times&#34;的字体,然后将其上传到Swift。
我还建议将标有"Times New Roman"
的部分更改为"Times"
如果它仍无效,那么您的代码就会出现问题。我能够从头开始重写你的脚本,它对我有用。如果它仍然无法正常工作,请尝试使用以下代码:
import UIKit
class ViewController: UIViewController {
var popupLabel = UILabel()
var popupBox = UIView()
override func viewDidLoad() {
view.addSubview(popupBox)
popupBox.addSubview(popupLabel)
popupBox.isHidden = true
popupLabel.isHidden = true
showPopup(CGPoint(x: 250 /* Change X Position */, y: 125 /* Change Y Position */), CGSize(width: 250 /* Change Width */, height: 100 /* Change Height */), "Blah" /* Change displayed text */)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func showPopup(_ rectPosition: CGPoint, _ rectSize: CGSize, _ popupDisplay: String) {
popupBox.isHidden = false
popupLabel.isHidden = false
popupLabel.textColor = UIColor.white
popupLabel.font = UIFont(name: "Times New Roman", size: 14.0)
self.popupLabel.text = popupDisplay
popupBox.frame.size = rectSize
popupBox.center = rectPosition
popupBox.backgroundColor = UIColor.darkGray
popupLabel.frame.size = popupBox.frame.size
popupLabel.textAlignment = .center
popupLabel.center = popupBox.center
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
如果您要使用此代码,请将其放在ViewController.swift
文件中,并考虑删除其他类。
答案 3 :(得分:0)
原件:
self.translatesAutoresizingMaskIntoConstraints
修正:
self.label.translatesAutoresizingMaskIntoConstraints