ios


UIView center property is giving unexpected result


I am trying to design the screen programmatically. First, I setup the navigation bar. After that I add a table view and after that I add a plain UIView. I am having trouble while using the .center property of this last added UIView. I want to add a UIActivityIndicatorView to this view. My code is as follows (using default height and width of large white activity indicator, which is 37pt each):
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)
}
In the above code, if you see carefully, I have commented POINT:1 POINT:2 and using POINT:3. This gives me following result (which is correct and as expected):
Now, when I use POINT:1 POINT:2 and comment POINT-3 as following:
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)
}
I get following result (which is , I guess, wrong and unexpected):
So exactly what is the difference my two approaches and what is exactly going wrong here? Is my understanding of this two approaches wrong? If yes then what exactly happens here?
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.
ViewDidAppear
viewDidAppear is called when the view is actually visible, so your
code have to place here //now check it

Related Links

When writing to text file (appending) the last value I write replaces all previous values
How to use WebP image in UIImageView Swift
How to get rid of the glitch that occurs right after pressing the back button?
Distance between two UIButtons according to screen size? [closed]
Creating ssl public and private key pair in ios app
How to Renaming a Project or App in XCode? [duplicate]
ios - how to have Multiple lines of NSAttributedString
Granting permission for the calendar without showing ios builtIn alertView
iOS JSON Parsing, Format Issues
UIButton in scrollView position unexpected
cross building binutils 2.25.1 for iOS8.4 (arm64-apple-darwin14.0.0)
SWIFT: Modal Segue From TableViewCell (Prototype Cell) Is Not Working
NSDictionary is nil when I decode with NSKeyedUnarchiver even when passing valid data object
Why are my Parse API requests increasing rapidly each day?
How to check valid url?
Automatic Login in IOS Swift [closed]

Categories

HOME
regex
uwp
asp.net-identity
f#
jsp
redirect
magento
xaml
tcl
watson
jsonschema
gluu
visual-studio-2012
jasypt
build.gradle
regression
graphengine
signature
condor
spring-cloud-consul
lookup-tables
html-email
exe4j
freetype
many-to-many
csom
jqplot
apache-httpcomponents
iar
valueinjecter
feedly
python-responses
stackexchange.exceptional
nib
microsoft-translator
flags
symfony-process
psychopy
btle
projection
tabview
sweet.js
rtems
keyboard-maestro
log4perl
sql-insert
clientscript
wrk
ctl
mashery
derbyjs
dr-memory
kie-workbench
wxhaskell
vistadb
ipopt
sound-synthesis
sizeof
sqlfiddle
rras
airplay
schematiq
flexigrid
php-opencloud
kinto
thinktecture-ident-server
clipping
ironmq
tun
axlsx
nbug
llvm-gcc
newsql
openxls
simpleadapter
horizontalscrollview
directcompute
bonecp
tlb
cfile
array-address
blueprint
floating-point-conversion
pubdate
dependency-walker
hudson-plugins
java-ee-5
domain-mapping
xsocket
documentviewer
datakey
code-structure
media-manager
graphiti-js
system-tray
apache-config
jboss-cache
greensoftware

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App