gpt4 book ai didi

ios - UIView center 属性给出了意想不到的结果

转载 作者:行者123 更新时间:2023-11-30 13:40:12 25 4
gpt4 key购买 nike

我正在尝试以编程方式设计屏幕。首先,我设置导航栏。之后我添加一个表格 View ,然后添加一个普通的 UIView。我在使用最后添加的 UIView.center 属性时遇到问题。我想向此 View 添加一个 UIActivityIndi​​catorView 。我的代码如下(使用大白色事件指示器的默认高度和宽度,各37pt):

func setupActivityView() {
let screen = UIScreen.mainScreen().bounds
let view = UIView(frame: CGRectMake(0.0, self.navbar.frame.height, screen.width, UIScreen.mainScreen().bounds.height-self.navbar.frame.height))
view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.80)
//view.hidden = true
self.activityView = view
self.view.addSubview(view)

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
spinner.startAnimating()
spinner.hidesWhenStopped = true
self.spinner = spinner
/*POINT:1*/ //spinner.center = view.center
/*POINT:2*/ //spinner.frame.size = CGSizeMake(37.0, 37.0)
/*POINT:3*/ spinner.frame = CGRectMake((view.frame.width/2) - 18.5, (view.frame.height/2) - 18.5, 37.0, 37.0)
view.addSubview(spinner)
}

在上面的代码中,如果你仔细看的话,我注释了POINT:1 POINT:2并使用了POINT:3。这给了我以下结果(这是正确的并且符合预期):

enter image description here

现在,当我使用 POINT:1 POINT:2 并注释 POINT-3 时,如下所示:

func setupActivityView() {
let screen = UIScreen.mainScreen().bounds
let view = UIView(frame: CGRectMake(0.0, self.navbar.frame.height, screen.width, UIScreen.mainScreen().bounds.height-self.navbar.frame.height))
view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.80)
//view.hidden = true
self.activityView = view
self.view.addSubview(view)

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
spinner.startAnimating()
spinner.hidesWhenStopped = true
self.spinner = spinner
/*POINT:1*/ spinner.center = view.center
/*POINT:2*/ spinner.frame.size = CGSizeMake(37.0, 37.0)
/*POINT:3*/ //spinner.frame = CGRectMake((view.frame.width/2) - 18.5, (view.frame.height/2) - 18.5, 37.0, 37.0)
view.addSubview(spinner)
}

我得到以下结果(我猜这是错误的且出乎意料的):

enter image description here

那么我的两种方法到底有什么区别,到底出了什么问题?我对这两种方法的理解是否错误?如果是,那么这里到底发生了什么?

最佳答案

  1. ViewDidload

    In this method view width and height you will not properly. You can print view width and height then you will know the reason.

  2. ViewDidAppear

    viewDidAppear is called when the view is actually visible, so your code have to place here //now check it

关于ios - UIView center 属性给出了意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com