简体   繁体   English

在iOS Playground应用程序的viewcontroller内部将视图居中

[英]Center a view inside viewcontroller in iOS Playground app

I'm currently building a swift playground book. 我目前正在制作一本快速的操场书。 Is it possible to center a view inside a view controller while running on an iPad ? 在iPad上运行时,是否可以将视图居中放置在视图控制器中?

Here's my loadView function of my view controller: 这是我的视图控制器的loadView函数:

override public func loadView() {
    let view = UIView()
    view.backgroundColor = .white
    view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
    self.view = view

    let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
    let newView = UIView(frame: frame)
    view.addSubview(newView)
}

How can I center the newView in the view of the view controller? 如何在视图控制器的视图中居中newView

Thanks ! 谢谢 !

You can try to do it using Autolayout: 您可以尝试使用自动版式进行操作:

let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
let newView = UIView(frame: frame)
view.addSubview(newView)
newView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    newView.leftAnchor.constraint(equalTo: view.leftAnchor),
    newView.rightAnchor.constraint(equalTo: view.rightAnchor),
    newView.widthAnchor.constraint(equalTo: newView.heightAnchor),
])
newView.backgroundColor = UIColor.red

UPDATE 更新

Playground code: 游乐场代码:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .red
        view.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
        self.view = view

        let frame = CGRect(x: 0, y: 0, width: 375, height: 375)
        let newView = UIView(frame: frame)
        newView.backgroundColor = .blue
        view.addSubview(newView)

        newView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            newView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            // need to define its size too
            newView.heightAnchor.constraint(equalToConstant: 375),
            newView.widthAnchor.constraint(equalTo: view.heightAnchor),
            ])
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

View Controller Lifecycle - slight difference between an Xcode Playground and an Xcode Project ... View Controller生命周期-Xcode Playground和Xcode Project之间的细微差别...

In this Xcode playground, notice the view controller's view bounds don't yet match the simulator size in viewDidLoad (). 在此Xcode游乐场中,请注意,视图控制器的view边界尚未与viewDidLoad ()中的模拟器大小匹配。 The simulator seems to default to iPad size but then display at iPhone size. 该模拟器似乎默认为iPad大小,但随后以iPhone大小显示。 In an Xcode project however, this isn't the case — you can get the current simulator bounds in viewDidLoad() . 但是,在Xcode 项目中,情况并非如此-您可以viewDidLoad()获得当前模拟器的边界。

In either case, a better place to position your subviews is in viewDidLayoutSubviews() . 无论哪种情况,在viewDidLayoutSubviews()放置子视图的最佳位置。 在此处输入图片说明

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

    var yellowView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        yellowView.backgroundColor = .yellow
        yellowView.frame = CGRect(x: 0, y: 0, width: 250, height: 250)
        view.addSubview(yellowView)
    }

    override func viewDidLayoutSubviews() {
        yellowView.center =  CGPoint(x: view.bounds.width / 2, y: view.bounds.height / 2)
    }
}

let vc = MyViewController()
PlaygroundPage.current.liveView = vc

像这样:

newView.center = self.view.center

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS自动布局:在ViewController中获取视图内部视图的宽度 - iOS autolayout: get width of views inside a view in ViewController (iOS)音频播放器作为应用程序中所有父视图控制器上的子视图控制器 - (iOS) Audio player as child viewcontroller on all parent view controllers in the app Swift IOS应用程序在哪里实例化View和ViewController? - Where does a Swift IOS app instantiate the View and ViewController? 适用于iOS测验应用程序的ViewController - ViewController for an iOS quiz app iOS ViewController调用容器内的子ViewController - ios viewcontroller to call child viewcontroller inside container iOS应用程序上的Swift按钮错误,但在操场上没有 - Swift button error on iOS app but not in playground iOS自动布局 - 将位于tableviewcell内的视图移动到屏幕中心 - iOS autolayout-move a view located inside a tableviewcell to the center of the screen 在水平滚动(iOS)时,如何将内容集中在显示的环形视图的准确中心的集合视图中? - How to center content inside collection view in exact center of shown ring view while scrolling horizontally (iOS)? ViewController和通知中心(视图,而不是设计模式:p) - ViewController and Notification Center (the view, not the design pattern :p) 在容器视图中加载 ViewController - Loading a ViewController inside a Container View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM