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
Related
I want bView to be free of shadows. How can I achieve this?
I want to shadow all subviews of the superView, so I cannot remove the shadow settings of the superView.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
superView.layer.shadowColor = UIColor.lightGray.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
self.view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
I have tried bView.clipsToBounds = true but it's not working.
If you give superView a non-clear background color then none of its subviews will get a shadow. You can then apply a shadow to the subviews that you want to have a shadow.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white // <-- Add this line
superView.layer.shadowColor = UIColor.black.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
// Add shadow to aView
aView.layer.shadowColor = UIColor.black.cgColor
aView.layer.shadowOffset = CGSize(width: 0, height: 3)
aView.layer.shadowOpacity = 0.2
aView.layer.shadowRadius = 5
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
If you have several views that need a shadow then I would define a method to make it easier:
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowOpacity = 0.2
layer.shadowRadius = 5
}
}
Then your code becomes something like this:
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white
superView.addShadow()
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
aView.addShadow()
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
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
I want to calculate view size with autolayout, but it can not get right value. Here is my code
// build views
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let posterView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 100))
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
titleLabel.text = "Title "
titleLabel.font = UIFont.systemFontOfSize(16) // label height = 19.5
view.addSubview(posterView)
view.addSubview(titleLabel)
posterView.snp_makeConstraints { (make) in
make.top.left.right.equalTo(view).offset(0)
make.width.equalTo(posterView.snp_height).multipliedBy(16.0 / 9.0)
}
titleLabel.snp_makeConstraints { (make) in
make.top.equalTo(posterView.snp_bottom).offset(0)
make.left.equalTo(view).offset(0).priority(750)
make.right.equalTo(view).offset(0).priority(750)
make.bottom.equalTo(view.snp_bottom).offset(0)
}
// calculate
view.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = NSLayoutConstraint(item: view, height: 90+19.5)
view.addConstraint(heightConstraint)
let fittingSize = view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
// the fittingSize is (40,109.5), not expect value (160, 109.5)
Please don't tell me how to calculate dynamic height for UITableViewCell. This is a opposite problem from height to width.
Thanks
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))
So I have a 60x60 button on my storyboard and set a class for it.
class CustomRedButton: UIButton {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.cornerRadius = 0.5 * self.bounds.size.width
self.backgroundColor = UIColor.redColor()
self.titleLabel!.font = UIFont(name: "Futura", size: 25)
self.titleLabel!.textAlignment = .Center
}
}
What I want to do is adjust the size of the button, and the font in it, based on the size of the screen.
Use view.bounds.maxX and view.bounds.maxY within the ViewController when you initialize it.
this is what I'm doing, but it only changes the width, not the height or the font size.
button1 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
button1.center = CGPoint(x: view.bounds.midX - button1.bounds.width / 2 - 5, y: view.bounds.maxY - 160)
button2 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
button2.center = CGPoint(x: view.bounds.midX + button1.bounds.width / 2 + 5, y: view.bounds.maxY - 160)
button3 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
button3.center = CGPoint(x: view.bounds.midX - button1.bounds.width / 2 - 5, y: view.bounds.maxY - 100)
button4 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
button4.center = CGPoint(x: view.bounds.midX + button1.bounds.width / 2 + 5, y: view.bounds.maxY - 100)
To do it for the height set the height to (view.bounds.maxY / 16 (or whatever number its up to you)) - 10 (spacing between buttons times 2) and change the center point to reflect the changes in height
To do the same for font size use view.bounds.maxX and divide it by something like 40. You get the idea.