Updating to latest Swift version problems - ios

I just updated to the latest version of Swift and I am getting issues with a breakpoint error. There are no errors in the console.
backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)
backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(backgroundsetting)
Then after the update it wanted me to set the backgroundsetting.frame as! CGRect and it crashes the app when I open that part of the app. Why is this occurring? Here is the after code:
backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)
backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) as! CGRect
self.view.addSubview(backgroundsetting)

The backgroundsetting.frame property is a type of CGRect, why not creating a CGRect like this:
backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)

If you are using constraint for your view and then use height:self.view.frame.size.height and width:self.view.frame.size.width , it will not show proper height and width as compare to your iPhone screen.
For that its better to use UIScreen size i.e UIScreen.main.bounds.height and UIScreen.main.bounds.width , it will always show original width and height of your iPhone.
backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) as! CGRect
self.view.addSubview(backgroundsetting)

Related

Change UILabel.frame when orientation changes using viewWillTransition

I programmatically create a set of buttons in the viewDidLoad() of my UIInputViewController. I am trying to change the size of these buttons when the orientation changes from landscape to portrait and vice-versa.
I am trying to do this by using
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
but when I go through the same code I used to create my buttons in viewDidLoad() with only the UIlabel.frame change to accommodate the new height and width, the view is not updated. Do I have to first remove the buttons and load them from scratch or is there another method to do this?
Below is the full code from the function.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
let width = screensize.width
var height = CGFloat(141)
let sizey = screensize.height
switch sizey {
case 1366:
height = CGFloat(383-15)
case 1112:
height = CGFloat(320-15)
case 812:
height = CGFloat(141-15)
case 1024:
height = CGFloat(320-15)
case 736:
height = CGFloat(226-15)
case 667:
height = CGFloat(216-15)
case 568:
height = CGFloat(216-15)
case 768:
height = CGFloat(420-15)
default:
height = CGFloat(141-15)
}
connection.frame = CGRect(x: 0, y: 0, width: width/2, height: 15)
serialNum.frame = CGRect( x: width/2, y: 0, width: width/2, height:15)
plus.frame = CGRect(x:0, y: 15, width: width/5,height: height/4)
minus.frame = CGRect(x:0, y: height/4+15, width: width/5,height: height/4)
left.frame = CGRect(x:0, y: height/2+15, width: width/5,height: height/4)
nextKeyboardButton.frame = CGRect(x:0,y: 3*height/4+15, width: width/5,height: height/4)
num1.frame = CGRect(x:width/5, y: 15, width: width/5,height: height/4)
num4.frame = CGRect(x:width/5, y: height/4+15, width: width/5,height: height/4)
num7.frame = CGRect(x:width/5, y: height/2+15, width: width/5,height: height/4)
comma.frame = CGRect(x:width/5, y: 3*height/4+15, width: width/5,height: height/4)
num2.frame = CGRect(x:width*2/5,y: 15, width: width/5,height: height/4)
num5.frame = CGRect(x:width*2/5,y: height/4+15, width: width/5,height: height/4)
num8.frame = CGRect(x:width*2/5,y: height/2+15, width: width/5,height: height/4)
num0.frame = CGRect(x:width*2/5,y: 3*height/4+15, width: width/5,height: height/4)
num3.frame = CGRect(x:width*3/5,y: 15, width: width/5,height: height/4)
num6.frame = CGRect(x:width*3/5,y: height/4+15, width: width/5,height: height/4)
num9.frame = CGRect(x:width*3/5,y: height/2+15, width: width/5,height: height/4)
period.frame = CGRect(x:width*3/5,y: 3*height/4+15, width: width/5,height: height/4)
back.frame = CGRect(x:width*4/5, y: 15, width: width/5, height: height/4)
spaceBar.frame = CGRect(x: width*4/5, y: height/4+15, width: width/5, height: height/4)
right.frame =
CGRect(x: width*4/5, y: height/2+15, width: width/5, height: height/4)
returnKey.frame = CGRect(x:width*4/5, y: height*3/4+15, width: width/5, height: height/4)
self.view.addSubview(spaceBar)
self.view.addSubview(num1)
self.view.addSubview(num2)
self.view.addSubview(num3)
self.view.addSubview(num4)
self.view.addSubview(num5)
self.view.addSubview(num6)
self.view.addSubview(num7)
self.view.addSubview(num8)
self.view.addSubview(num9)
self.view.addSubview(num0)
self.view.addSubview(plus)
self.view.addSubview(minus)
self.view.addSubview(back)
self.view.addSubview(left)
self.view.addSubview(right)
self.view.addSubview(period)
self.view.addSubview(comma)
self.view.addSubview(nextKeyboardButton)
self.view.addSubview(returnKey)
You need to use the ‘size’ argument that you get in the ‘viewWillTransition’ method to set the new ‘width’ and ‘sizey’ properties in stead of ‘screensize’.
You shouldn't call addSubview on orientation change if you've already added the buttons in viewDidLoad. Updating the frame should be enough.
Since you have added these set of buttons in the viewDidLoad() of your UIInputViewController, you don't need to use addSubview in viewWillTransition to size method.
Even after rotating the device still you are using the old screen size.
Change
let width = screensize.width
let sizey = screensize.height
To
let width = UIScreen.main.bounds.width
let sizey = UIScreen.main.bounds.height
Or
let width = size.width
let sizey = size.height

UIScrollView Swift - Adding Images

I'm trying to get a simple color picker by adding the colors as Images into an UIScrollView.
I've used UIScrollViews before and never encountered any issues. This time nothing gets displayed no matter what I do. Double checked if the colors get picked right and everything seems to be fine. For some reason the colors don't get added to the UIScrollView.
func fillColorPicker(colors: Array<String>){
for i in 0..<colors.count{
let imageView = UIImageView()
imageView.image = UIImage(named: colors[i])
imageView.contentMode = .scaleAspectFit
let xPosition = CGFloat(i) * imageView.frame.width
imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
colorPicker.addSubview(imageView)
}
}
Found something. Somehow the imageView.frame.width is always 0. I assumed it would take the width from the image file?
Solution:
func fillColorPicker(colors: Array<String>){
for i in 0..<colors.count{
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 75, height: 75))
imageView.image = UIImage(named: colors[i])
imageView.contentMode = .scaleAspectFit
let xPosition = CGFloat(i) * imageView.frame.width
imageView.frame = CGRect(x: xPosition, y: 0, width: imageView.frame.width, height: self.colorPicker.frame.height)
colorPicker.contentSize.width = imageView.frame.width * CGFloat(i+1)
colorPicker.addSubview(imageView)
}
}

Reduce height of a UIView from the top

I am simply trying to set frame of a UIView, but when you try to reduce the height of view, it will be reduced from the bottom of the view, not the top. How can I change hight of view from the top of it not bottom ?
customView = CGRect(x: 0, y:0 , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
You can update frame.origin.y as much as the height.
if reduceHeightSize is positive;
customView = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y + reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
if you store it negative reduceHeightSize;
customView = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y - reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height + reduceHeightSize)
It would be better to just keep amount of the size not the direction.
You need to do like...
customView = CGRect(x: 0, y:reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
customView = CGRect(x: 0, y:0 , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
The problem of your code is that you change Height in CGRect. and you want to change top position of view so you need to change position of also y of your view so finally your code will be
customView = CGRect(x: 0, y: reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height - reduceHeightSize)
If I understand your question correctly, that's probably because your view is not constrained/anchored at the bottom and thus defaulting to reduce on the top. Try adding a bottom constraint.
I just figured it out what I need:
customView.frame = CGRect(x: 0, y: self.view.frame.origin.y - reduceHeightSize , width: self.view.frame.width , height: self.view.frame.height + reduceHeightSize)

Different sizes of scrollview pages

I like to set one page from my UIScrollView bigger than the others. The last page should be the same size like the whole UIView, is that possible?
That's my unfinished code:
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let viewHeight = self.view.frame.height
let viewWidth = self.view.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
imgTree.image = UIImage(named: "preview3")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgTree)
self.scrollView.addSubview(feedbackView)
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true
The feedbackView is the one, which should be the biggest.
I don't know if this will give you the type of result you want, but something to try...
Instead of adding feedbackView to your scrollView, add another UIScrollView as the 4th subview, and then add feedbackView as a subview of that scroll view:
// your original setup code
self.scrollView.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width
let viewHeight = self.view.frame.height
let viewWidth = self.view.frame.width
let imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let imgTree = UIImageView(frame: CGRect(x: scrollviewWidth*2, y: 0, width: scrollviewWidth, height: scrollviewHeight))
let feedbackView = UIView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: viewWidth, height: viewHeight))
imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")
imgTree.image = UIImage(named: "preview3")
self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgTree)
// don't add feedbackView to scrollView
// self.scrollView.addSubview(feedbackView)
// instead, create another scroll view,
// add feedbackView to that scroll view,
// and add that scroll view as the 4th "page" in scrollView
let fbScrollView = UIScrollView(frame: CGRect(x: scrollviewWidth*3, y: 0, width: scrollviewWidth, height: scrollviewHeight))
// just so we can see where it is during development
fbScrollView.backgroundColor = .purple
// feedbackView will now be in its own scroll view, so
// set its origin to 0,0
feedbackView.frame.origin = CGPoint.zero
fbScrollView.addSubview(feedbackView)
fbScrollView.contentSize = feedbackView.frame.size
// add the new scroll view to the "paging" scrollView
self.scrollView.addSubview(fbScrollView)
// and the rest of your setup
self.addChildFeedBack(feedbackView: feedbackView)
self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 4, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true

AutoLayout for programmatically created uiview element

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))

Resources