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

Why can't I bold just part of a string in Swift with NSMutableString?
Swift - WebView doesn't detect swipes when loading
Interstitial DFP ad doesn't fill the screen
Q:UITableView scrollview delegate method did not pass the special code
ios Xcode storyboard compact not working
iOS - Where is object stored in application NSUserDefault? [duplicate]
UIImage to NSData fatal error: unexpectedly found nil while unwrapping an Optional value
How to load images for UITableView or UICollectionView from cache everytime
iOS - UITableview cells with shared surface
Xcode fix memory problems
UIWebview remove padding / margin
How do I not get optional in the output?
Material Select blinking on iOS
How to get the Core data Primary key value in ios Swift? [duplicate]
How to remove only minutes from UIDatePickerMode.DateAndTime
UIButton click causing app freeze/Rapid increase in memory

Categories

HOME
amazon-s3
azure
wix
unit-testing
rtmp
isis
android-dialer
google-analytics-api
socrata
struts2-jquery
wav
avplayer
openui5
postgresql-9.5
deserialization
raspberry-pi2
spring-boot-admin
stackexchange-api
thrift
silk-performer
formio
co
italic
json-c
udid
scom
dashboard-designer
subtotal
nav
gridstack
jackrabbit
cookiestore
uistoryboardsegue
signing
spring-saml
sagepay
symfony-process
consistency
maven-jetty-plugin
sql-insert
jasmine2.0
database-cleaner
dump
abbyy
masm32
singlepage
gwt-material-design
topojson
ubuntu-15.04
xendesktop
agents-jade
telecommunication
pager
securesocial
intrinsics
accounts
domo
sejda
biojava
hypervisor
appv
node-orm2
cvc4
pegkit
boost-coroutine
diagonal
chatjs
bit.ly
repaint
concurrentdictionary
email-spam
keymapping
angularjs-google-maps
libnids
jquery-data
geometry-surface
dynamic-usercontrols
hudson-plugins
xsocket
msxsl
self-organizing-maps
intel-8080
katta
closest
adbwireless
xquery-update
variable-names
usenet
plumtree

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