Tag: swift3

YouTubeembedded的video显示大小错误

我在UIWebView中embedded了一个YouTubevideo,以显示在我的应用程序中。 以下是代码: func loadVideo(){ if let youtubeCode = exerciseYoutubeCode { guard !youtubeCode.isEmpty else { return } exerciseVideoWebView.isHidden = false exerciseVideoWebView.allowsInlineMediaPlayback = true exerciseVideoWebView.scrollView.isScrollEnabled = false exerciseVideoWebView.scrollView.bounces = false exerciseVideoWebView.loadHTMLString("<html><head><style>body{margin:0px}</style></head><body><iframe width=\"\(exerciseVideoWebView.frame.width)\" height=\"\(exerciseVideoWebView.frame.height)\" src=\"https://www.youtube.com/embed/\(youtubeCode)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe></body></html>", baseURL: nil) } } 问题是,embedded的video似乎并不尊重我传递给iframe的UIWebView的宽度和高度。 在模拟器中,video大约比UIWebView大15-25个像素。 我可以忍受这一点,但是在物理设备上运行时,它比UIWebView小15-25个像素,UIWebView的右侧和底侧都会显示白色的HTML页面。 UIWebView具有16:9的自动约束,与YouTube的video宽高比相匹配。 如果我把下面的脚本放到HTML中,我可以看到,videoiframe的宽度和高度与打印到UIWebView的控制台的高度和宽度完全匹配: <script>setTimeout(function(){ alert(document.getElementsByTagName('iframe')[0].offsetWidth + ' ' + document.getElementsByTagName('iframe')[0].offsetHeight)},10000) </script> 很显然,如果HTML认为其尺寸与UIWebView相同,就会出现某种缩放,但我无法弄清楚如何解决这个问题。

实例化存储在metatype字典中的类

我已经按照制作一个Swift字典的关键是“types”的解决scheme? 创build可以使用类types作为键的字典。 我想要做的是:我有一个字典,应该存储类types与他们的类types(又名元types)作为键: class MyScenario { static var metatype:Metatype<MyScenario> { return Metatype(self) } } var scenarioClasses:[Metatype<MyScenario>: MyScenario.Type] = [:] 然后我有方法来注册和执行scheme: public func registerScenario(scenarioID:MyScenario.Type) { if (scenarioClasses[scenarioID.metatype] == nil) { scenarioClasses[scenarioID.metatype] = scenarioID } } public func executeScenario(scenarioID:MyScenario.Type) { if let scenarioClass = scenarioClasses[scenarioID.metatype] { let scenario = scenarioClass() } } 问题是在最后一行: 使用元types值构造类types为“MyScenario”的对象必须使用“必需的”初始值设定项。 看起来编译器很困惑,因为我不能在这个任务中使用'required'。 有没有人有一个想法,我将不得不在executeScenario()实例化scenarioClass ?

无法将typesPromise(_,_) – > DataRequest的返回expression式转换为返回typesPromise <DataResponse,AnyObject >>

无法将typesPromise( , ) – > DataRequest的返回expression式转换为返回typesPromise> 我的function是 func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<AnyObject>> { var request = URLRequest(url: URL(string: url)!) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = try! JSONSerialization.data(withJSONObject: parameters) return Promise { fulfill, reject in manager.request(request) .responseJSON { response in fulfill(response) } 我在返回Promise行时遇到这个错误。 我如何转换? 我试图改变我的返回签名Promise<DataRequest, Error并得到一个编译错误,该承诺太专门用2参数而不是1。

如何使用以下方法发布图片数组

我正在使用Swift 3,并想用这种方法张贴图片数组 – 请帮助我该怎么做 if let imageData = UIImageJPEGRepresentation(customKindImage.image! , 1) { print("There is Not Any Images to upload!") let uploadScriptUrl = URL(string : "http://www.swiftdeveloperblog.com/http-post-example-script/") var request = URLRequest(url: uploadScriptUrl!) request.httpMethod = "POST" request.setValue("Keep-Alive" , forHTTPHeaderField : "Connection") let task = URLSession.shared.uploadTask(with: request, from: imageData, completionHandler: { (data, response, error) in guard let _ = […]

使用Swift 3和Realm同步Apple Watch和iPhone

我需要从苹果手表和iPhone显示和修改我的数据结构。 数据库: 我目前正在使用一个简单的领域结构,我有一个对象A和一个对象B可以容纳大量的A。 所以在iPhone上,用户可以创build一个B并添加A,然后查看所有的A和B。 我想让苹果手表显示当前B的所有A,并给予用户在当前B上添加新A的机会。 我试图这样做的方式: 我想从iPhone移动洞Realm文件到手表或其他方式。 (这是来自互联网的一个提示) iPhone代码: override func viewDidLoad() { super.viewDidLoad() if WCSession.isSupported() { //makes sure it's not an iPad or iPod let watchSession = WCSession.default() watchSession.delegate = self watchSession.activate() transferRealmFile() if watchSession.isWatchAppInstalled { do { try watchSession.updateApplicationContext(["foo": "bar"]) } catch let error as NSError { print(error.description) } } } } func […]

滚动时,UITableViewCell的选定button会消失

我正在使用swift 3编码的项目,我有一个UITableViewCell里面的UIButton,在点击button后图像会改变。 虽然所选的button被按照预期select,但是一旦UITableview滚动select的图像就会消失,因为这些单元已被重新使用。 由于我刚刚接触编程时遇到了编写逻辑的麻烦。 帮助将非常感谢代码如下。 CellFoRow func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) //Button_Action addSongButtonIdentifier(cell: cell, indexPath.row) } 这是单元格被创build的地方。 func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int) { let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! UIButton //accessibilityIdentifier is used to identify a particular element which takes an input parameter […]

如何在swift 3中考虑filePath来显示和显示pdf文件?

我是iOS开发新手。 在我的项目中,我在tableview上显示exerciseFilePath。 回应如下。 "exerciseId" : 1, "exerciseName" : "Fitness Exercise", "exerciseFilePath" : "\/p\/pdf\/exercise_pdf\/fitness_exercise.pdf" 我需要在didSelectRowAtIndexpath的另一个视图中显示PDF。 我不知道如何显示PDF,以及要遵循的步骤来显示该PDF。 我希望你能理解我的问题。 请帮助我如何做到这一点。

错误'缺less必要的参数:grant_type'使用Swift 3

我正在Twitter的API,我想获得access_Token,但我得到这个错误: { "errors" : [ { "message" : "Missing required parameter: grant_type", "label" : "forbidden_missing_parameter", "code" : 170 } ] }. 我的要求如下: let dict = ["grant_type" : "client_credentials"] requestPOSTURL("https://api.twitter.com/oauth2/token", params: dict as [String : AnyObject] , headers: ["Authorization" : "Basic " + strHeader, "Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8"], success: { (json) in Helper.sharedInstance.Print(json as AnyObject) }, failure: […]

如何在Swift 3中更改TabBar中禁用的项目颜色?

我有一个TabBar在我的项目,当用户select项目将禁用,但颜色也将改变! 我想要的颜色与其他项目颜色相同,我用TabBar item.isEnabled = false 禁用项目,但此代码在这里不适用于此项目 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tabBar.barTintColor = UIColor.init(red: 126/255, green: 0/255, blue: 64/255, alpha: 1.0) if #available(iOS 10.0, *) { self.tabBar.unselectedItemTintColor = UIColor.white self.tabBar.unselectedItemTintColor = UIColor.white } else { // Fallback on earlier versions } UITabBar.appearance().tintColor = UIColor.white }

在自定义单元格中获取标签的高度,以便在我的主视图控制器中使用heightForRowAt

我有一个视图控制器,使用一个名为searchCell自定义单元格带内容,我想知道如何获得标签lblLeft的高度,并在tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath)中使用它 – > CGFloat函数返回标签的高度。 我在我的主视图控制器中使用的代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if searchMode == searching { let cell: MHSearchCell = tableView.dequeueReusableCell(withIdentifier: MHSearchCell.ReusableIdentifier()) as! MHSearchCell cell.helper = helpers[indexPath.row] cell.delegate = self return cell } } 在我的MHSearchCell中创build标签的代码的一部分,标签在这里连接成一个IBOutlet: lblLeft.text = "" if let expertiseCount = helper.expertise { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing […]