我是新来的Xcode,我不知道如何设置webview在safe areaiPhone X.我通过各种情侣答案已经走了,但他们并没有帮助。我对这个应用程序的唯一了解是,视图是以编程方式设置的。下面是我的ViewController.swift文件。
import UIKit
import WebKit
import AVFoundation
class ViewController: UIViewController, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate
{
var webView = WKWebView()
var containerView = WKWebView() //Footer Webview
override func loadView()
{
super.loadView()
}
override func viewDidLoad()
{
super.viewDidLoad()
let theConfiguration = WKWebViewConfiguration()
//Bind Swift and Javascript interface through MessageHandler IOSJSObject
theConfiguration.userContentController.add(self, name: "IOSJSObject")
/* Webview intialization Start */
webView = WKWebView(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-65), configuration: theConfiguration)
containerView = …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的UIControl,我实现为:
required init(coder: NSCoder) {
super.init(coder: coder)
initSubComponents()
}
func initSubComponents() {
// Display UIControl border for visual debugging
self.layer.borderColor = UIColor.redColor().CGColor
self.layer.borderWidth = 3.0
subviewContainer = UIView(frame: self.bounds.rectByInsetting(dx: 0, dy: 0))
// Display container border for visual debugging
subviewContainer.layer.borderColor = UIColor.redColor().CGColor
subviewContainer.layer.borderWidth = 3.0
println("UIControl frame: \(self.frame)")
println("subviewContainer frame: \(subviewContainer.frame)")
}
Run Code Online (Sandbox Code Playgroud)
要么
override func drawRect(rect: CGRect) {
initSubComponents()
// Code to add those subviews into this UIControl
}
func initSubComponents() {
// Display UIControl border for visual debugging
self.layer.borderColor …Run Code Online (Sandbox Code Playgroud) 我试图找到为什么自定义UIButton集合不起作用.在我之前的文章中描述的版本中,我在Swift 3中以编程方式创建了一个UIButton圆圈,并将圆圈固定在屏幕的中心.该版本使用了UIView的子类 - 基于Apple Swift教程(实现Button Action) - 以及利用Imanou Petit优秀代码示例(No.6 Anchor)的autolayout实现.在那个版本中,我设法让我的按钮在iPhone旋转时成功旋转,但按钮动作目标无法工作.
所以我现在尝试使用viewcontroller而不是UIView的子类的替代版本.这次相同的按钮动作目标有效,但旋转手机会使图像偏离中心,如下所示.
每次旋转时,以下消息也会在Xcode的调试区域中出现两次.
***[App] if we're in the real pre-commit handler we can't
actually add any new fences due to CA restriction***
Run Code Online (Sandbox Code Playgroud)
该消息在四个中发生三次,即当手机上下颠倒时没有消息.当我运行上一篇文章中的代码或下面显示的代码时,会发生这种情况.并且在每种情况下,"倒置"框是否已选中或未选中都没有区别.
我也试过禁用OS_ACTIVITY MODE但除了隐藏可能解释问题的消息之外什么都没改变.更有经验的人比我有希望认识一下这个调试消息无论是在我以前的代码(上下文意思此处所示)或我最新的代码,如下图所示.
原始代码
import UIKit
class ViewController: UIViewController {
// MARK: Initialization
let points: Int = 10 // 80 25 …Run Code Online (Sandbox Code Playgroud) I'm adding a subview(NSView), here is my code:
override func viewDidAppear() {
self.view.needsDisplay = true
let newView = NSView()
newView.autoresizesSubviews = true
newView.frame = view.bounds
newView.wantsLayer = true
newView.layer?.backgroundColor = NSColor.green.cgColor
view.addSubview(newView)
}
Run Code Online (Sandbox Code Playgroud)
And it works fine
But when I resize the window the subview is not resizing.
Any of you knows why or how can make the subview resize with superview?
I'll really appreciate your help
我对 swift 和 IOS 非常陌生,我想以编程方式将两个标签并排水平居中放置。下面是我的代码
let secondLabel : UILabel = {
let label = UILabel()
label.text = "1234"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.blue
return label
}()
let thirdLabel : UILabel = {
let label = UILabel()
label.text = "5678"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.red
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupLayout()
}
private func setupLayout(){
view.addSubview(secondLabel)
//secondLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
secondLabel.centerYAnchor.constraint(equalTo: …Run Code Online (Sandbox Code Playgroud) 使用锚样式NSLayoutConstraints(如在此答案:Swift |通过编程添加约束)如何为子视图设置动画?
对于那些懒惰的人,这里的代码是:
override func viewDidLoad() {
super.viewDidLoad()
let newView = UIView()
newView.backgroundColor = UIColor.redColor()
newView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(newView)
let horizontalConstraint = newView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
let vertivalConstraint = newView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor)
let widthConstraint = newView.widthAnchor.constraintEqualToAnchor(nil, constant: 100)
let heightConstraint = newView.heightAnchor.constraintEqualToAnchor(nil, constant: 100)
NSLayoutConstraint.activateConstraints([horizontalConstraint, vertivalConstraint, widthConstraint, heightConstraint])
}
Run Code Online (Sandbox Code Playgroud)
具体来说,如果我想newView向右滑动,该怎么做?谢谢。
我似乎无法找到合适的代码.我想在状态栏下放置20个图像(这将是Y)并将其居中(当然这将是X).我可以轻松地使用故事板来完成它,但我正在努力以编程方式进行.我认为这是我的代码:
var image: UIImage = UIImage(named: "someImage.png")!
var bgImage = UIImageView(image: image)
//Tried with self.view.frame.size.height but not working
bgImage.frame = CGRect(x: 0, y: self.view.frame.size.height - 20, width: self.view.frame.size.width, height: 64)
//also tried this which not worked as well
// bgImage.center = CGPointMake(self.view.frame.width, self.view.frame.hight - 20)
self.view.addSubview(bgImage)
Run Code Online (Sandbox Code Playgroud)
我已经搜索了苹果文档,但它很不清楚,任何帮助都会受到赞赏.
在UIView我有一个方法presentView():
public func presentView() {
UIApplication.sharedApplication().delegate?.window??.addSubview(self)
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.backgroundColor = UIColor.yellowColor()
addSubview(blurEffectView)
let topConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: blurEffectView, …Run Code Online (Sandbox Code Playgroud)