0

When i try to create an uiview with loading text and activityindicator, even though it looks centered in iphone 6, it is not centered in iphone 5.

How can i use auto layout for this programatically created UIView ?

I am trying to center this uiview in all screen sizes

boxView = UIView(frame: CGRect(x: TableView.frame.midX - 60, y: TableView.frame.midY - 15, width: 180, height: 50))
boxView.backgroundColor = UIColor.blackColor()
boxView.alpha = 0.5
boxView.layer.cornerRadius = 10

var activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
activityView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityView.startAnimating()

var textLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 200, height: 50))
textLabel.textColor = UIColor.whiteColor()
textLabel.text = "Loading"

boxView.addSubview(activityView)
boxView.addSubview(textLabel)

TableView.addSubview(boxView) 

EDIT: I tried this and it seems working good

var bounds: CGRect = UIScreen.mainScreen().bounds
        var width:CGFloat = bounds.size.width
        var height:CGFloat = bounds.size.height
        boxView = UIView(frame: CGRect(x: width/2 - 90, y: height/2 - 100, width: 180, height: 50))
Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130
  • Why don't you use the interface builder and create the necessary constraints there? – Thomas Mar 31 '15 at 01:08
  • possible duplicate of [Creating layout constraints programmatically](http://stackoverflow.com/questions/12826878/creating-layout-constraints-programmatically) – nhgrif Mar 31 '15 at 01:18
  • You are using hardcoded frame co-ordinates to place view, this will not work for all the devices. You need to use Auto Layout. Please take some time and read about it, that will make life lot easier. – Abdullah Mar 31 '15 at 01:55
  • @nhgrif that question is for object-c not Swift – William Kinaan Oct 05 '15 at 15:08

0 Answers0