我有一个UILabel以编程方式制作的:
var label = UILabel()
Run Code Online (Sandbox Code Playgroud)
然后,我为标签声明了一些样式,包括字体,例如:
label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30)
label.font = UIFont(name: "Typo GeoSlab Regular Demo", size: 15)
label.textColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 91/100, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
标签的第一部分将始终显示:"Filter:"然后是字符串的另一部分,例如"最受欢迎"
我希望单词过滤器以粗体显示,所以整个过程看起来像:
过滤:最受欢迎
我想以最简单的方式创建这种效果.我一直在互联网上搜索如何实现这一点,有很多方法,有些看起来像代码页.其中大部分似乎都是在Objective-C中.我想在Swift请:)
我不知道我是否在正确的路线上,但这NSRange有助于实现吗?提前致谢
更新
我使用一系列if语句来改变我的label变量.如:
if indexArray == 1 {
label.text = "Filter: Film name"
} else if indexArray == 2 {
label.text = "Filter: Most popular"
} else if …Run Code Online (Sandbox Code Playgroud) 我添加了一个UISearchBar到我的顶部PFQueryTableViewController.我已将searchBar的颜色更改为我的应用程序的颜色,但在执行此操作时,它似乎也将其右侧的"取消"按钮的颜色更改为相同的颜色.理想情况下,我希望颜色为白色.
此图显示了当前的外观:
它看起来没有"取消"按钮,但它有与searchBar相同的颜色(你仍然可以按它等)
有没有办法让我将这个'取消按钮的颜色改为白色?我尝试的一切似乎没有任何区别.
代码我用来制作UISearchBar这种颜色是:
UISearchBar.appearance().barTintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
UISearchBar.appearance().tintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
在故事板中我设置了这些:
最后,为了在SearchBar中制作占位符和文本白色,我使用了:
for subView in self.filmSearchBar.subviews {
for subsubView in subView.subviews {
if let searchBarTextField = subsubView as? UITextField {
searchBarTextField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search Cinevu film reviews", comment: ""), attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
searchBarTextField.textColor = UIColor.whiteColor()
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!:)
所以我有一个我在main.Storyboard中添加的Button,以及View的主背景,然后在viewController.swift文件中,我创建了一个UIView矩形形状,我想要坐在该按钮后面(作为它的背景).
问题是,当我添加矩形UIView时,它总是将它添加到按钮前面.所以我在网上研究并找到了sendSubviewToBack代码.我已添加此内容,但它会将其一直发送到视图的主UIImage背景之后.
有没有办法我可以在按钮后面发送这个UIView但在UIImage背景前?
func nextBarDisplayed() {
let theHeight = self.view.frame.height
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: theHeight - 50, width: self.view.frame.width, height: 50)
self.view.insertSubview(nextBar, aboveSubview: myBackgroundView)
}
Run Code Online (Sandbox Code Playgroud) 所以我的概念很简单,我有一个文本字段和一个标有"下一步"的按钮.如果用户没有在文本字段中放置任何内容,我希望将"下一步"按钮禁用给用户.为此,我运行了以下代码:
@IBAction func nextButton(sender: UIButton) {
if textField.text.isEmpty {
buttonLabel.userInteractionEnabled = false
}
}
Run Code Online (Sandbox Code Playgroud)
这会根据需要禁用按钮,但问题是,如果文本然后在文本字段中输入,则按钮仍处于禁用状态.我已经尝试在"if"语句之后添加"else"语句来反转我所说的是否有效,但事实并非如此.
任何帮助非常感谢.
缺口
我的代码:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
func imageEffect() {
// Set vertical effect for background
var verticalMotionEffect : UIInterpolatingMotionEffect =
UIInterpolatingMotionEffect(keyPath: "center.y",
type: .TiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -20
verticalMotionEffect.maximumRelativeValue = 20
// Set horizontal effect for background
var horizontalMotionEffect : UIInterpolatingMotionEffect =
UIInterpolatingMotionEffect(keyPath: "center.x",
type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -20
horizontalMotionEffect.maximumRelativeValue = 20
// Create group for background to combine both …Run Code Online (Sandbox Code Playgroud) 好的,所以我想在我的应用程序中播放电影预告片.用户将按下按钮,然后播放视频.我已将import AVKit和import AVFoundation行添加到我的文件中.这是我到目前为止制作视频的代码:
@IBAction func playTrailerPressed(sender: AnyObject) {
let videoURL = NSURL(string: "https://youtu.be/d88APYIGkjk")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是推出一个AVPlayerViewController,但不播放来自YouTube的视频.相反,我得到以下内容:
我尝试过来自YouTube的共享和嵌入链接,但都没有工作.如果我使用最后有视频文件名的链接,它可以正常播放,例如:"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"- 所以我知道代码有效.
有没有人知道这是否有可能以这种方式使用YouTube视频?(我也试过IMDb和Apple的预告片,但它是一样的).
谢谢你的帮助!
我创造了一个ContactUsViewController.
在此控制器中,用户将从a中选择一个选项pickerView,然后在a中键入消息,textView然后按"发送电子邮件"按钮.
当他们按下按钮时,它会创建一个,MFMailComposeViewController以便他们可以发送电子邮件.现在,当电子邮件发送,保存,取消或失败时,MFMailComposeViewController关闭它们在我的应用程序中.我想要一个警报然后似乎给他们一个更新,关于刚刚发生的事情.我最初使用UIAlertView设置了这个,并将此代码放在fun mailComposeController函数中,如下所示:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
NSLog("Email cancelled")
case MFMailComposeResultSaved.rawValue:
NSLog("Email saved")
let sendMailErrorAlert = UIAlertView(title: "Email saved", message: "Your email has been saved in Mail.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
case MFMailComposeResultSent.rawValue:
NSLog("Email sent")
let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
presentViewController(alertController, animated: …Run Code Online (Sandbox Code Playgroud) uialertview ios swift uialertcontroller mfmailcomposeviewcontroller
我想知道如何以编程方式创建分组/分段表格视图。我试图创建的设计是这样的:
我希望UIImageView在顶部具有160的高度,然后是3个部分,每个部分具有不同的行数。然后,我将在每行中放置静态标签,然后使用我的数据更新其标签。
我为此VC编写的代码如下:
import UIKit
class SelectedFilmTableViewController: UITableViewController {
var film: Film? {
didSet {
navigationItem.title = film?.Film_Name
}
}
var cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) …Run Code Online (Sandbox Code Playgroud) 我对Swift中的绘图并不是很了解,但我希望创建一个与广场略有不同的应用背景的一部分.请参阅下面的图片,了解我正在尝试制作的内容:
这个想法是,它将位于屏幕的顶部,只不过是背景.它会拉伸到屏幕的一半.然后我会在顶部等处添加图像.我需要在广场底部的点始终位于屏幕中间.
我可以通过代码在Swift中创建这个形状/ UIView吗?如果是这样,怎么样?
或者,将它创建为图像并以此方式执行是否更好?
我目前使用 UITableViewController (PFQueryTableViewController),我想在 TableView 的顶部显示一个 UIView。
理想情况下,我想在故事板中执行此操作,因此我可以轻松地向其中添加其他标签/按钮。但是,我似乎无法弄清楚如何或是否可以在故事板中做到这一点。那正确吗?
我已经以编程方式尝试过。我首先在顶部创建了变量:
var filterLabelView:UIView = UIView()
然后我在中添加了以下内容ViewDidLoad:
filterLabelView.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2, width: self.view.frame.width, height: 40)
filterLabelView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)
filterLabelView.backgroundColor = UIColor.redColor()
self.view.addSubview(filterLabelView) // See below
Run Code Online (Sandbox Code Playgroud)
我也试过:
self.view.insertSubview(filterLabelView, aboveSubview: tableView)
Run Code Online (Sandbox Code Playgroud)
这会创建红色的 UIView,但它似乎嵌入到 tableview 中,因为当我上下滚动时,View 会随之移动。
我想创造什么
理想情况下,我希望视图位于屏幕底部,并且在用户滚动时不移动。我希望它看起来如何的示例屏幕截图如下:
我已经读过最好的方法是使用一个 UIViewController 里面有一个 UITableView,但我宁愿不采用这种方法来查看我的应用程序已经构建了多少。
谁能帮我创建这个外观?提前致谢
所以我有一个textField,当用户按下时,它显示一个UIPickerView,它是从数组中填充的。
pickerView的顶部是否可能有搜索栏,以便用户可以在pickerView中搜索内容?
我之前从未见过这样做,所以不知道是否可能吗?
提前致谢。
我创建了一个UIView(它只是一个蓝色矩形),我希望它能粘在屏幕框架/视图的底部.
var nextBar = UIView()
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: 600, width: self.view.frame.width, height: 50)
self.view.addSubview(nextBar)
Run Code Online (Sandbox Code Playgroud)
我假设我需要在CGRect中的'y:'部分之后添加一些内容,但我似乎无法使其工作或找到任何能够在swift中向我显示此内容的内容.
提前致谢
我正在尝试从用户当前位置检索温度.
我正在使用OpenWeatherMap的API.问题是,他们提供默认的开尔文温度,我希望它在摄氏度.
我明白我只需要从开尔文价值中减去273.15 .... 但我正在努力弄清楚在哪里这样做.
我设置标签的代码:
var jsonData: AnyObject?
func setLabels(weatherData: NSData) {
do {
self.jsonData = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as! NSDictionary
} catch {
//handle error here
}
if let name = jsonData!["name"] as? String {
locationLabel.text = "using your current location, \(name)"
}
if let main = jsonData!["main"] as? NSDictionary {
if let temperature = main["temp"] as? Double {
self.tempLabel.text = String(format: "%.0f", temperature)
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题,因为我真的不知道从哪里开始,谢谢.
如果您需要查看更多我的代码,请告诉我们.
swift ×12
ios ×11
uiview ×4
uisearchbar ×2
uitableview ×2
uitextfield ×2
cgrect ×1
double ×1
iphone ×1
is-empty ×1
json ×1
mfmailcomposeviewcontroller ×1
nsrange ×1
string ×1
uialertview ×1
uibutton ×1
uilabel ×1
uipickerview ×1
youtube ×1