diff --git a/SCLAlertView/SCLAlertView.swift b/SCLAlertView/SCLAlertView.swift index 8b7ac34..a9d112d 100644 --- a/SCLAlertView/SCLAlertView.swift +++ b/SCLAlertView/SCLAlertView.swift @@ -76,6 +76,8 @@ public class SCLAlertView: UIViewController { let kWindowWidth: CGFloat = 240.0 var kWindowHeight: CGFloat = 178.0 var kTextHeight: CGFloat = 90.0 + let kTextFieldHeight: CGFloat = 45.0 + let kButtonHeight: CGFloat = 45.0 // Font let kDefaultFont = "HelveticaNeue" @@ -170,28 +172,50 @@ public class SCLAlertView: UIViewController { } // Set background frame view.frame.size = sz + + // computing the right size to use for the textView + let maxHeight = sz.height - 100 // max overall height + var consumedHeight = CGFloat(0) + consumedHeight += kTitleTop + kTitleHeight + consumedHeight += 14 + consumedHeight += kButtonHeight * CGFloat(buttons.count) + consumedHeight += kTextFieldHeight * CGFloat(inputs.count) + let maxViewTextHeight = maxHeight - consumedHeight + let viewTextWidth = kWindowWidth - 24 + let suggestedViewTextSize = viewText.sizeThatFits(CGSizeMake(viewTextWidth, CGFloat.max)) + let viewTextHeight = min(suggestedViewTextSize.height, maxViewTextHeight) + + // scroll management + if (suggestedViewTextSize.height > maxViewTextHeight) { + viewText.scrollEnabled = true + } else { + viewText.scrollEnabled = false + } + + let windowHeight = consumedHeight + viewTextHeight // Set frames var x = (sz.width - kWindowWidth) / 2 - var y = (sz.height - kWindowHeight - (kCircleHeight / 8)) / 2 - contentView.frame = CGRect(x:x, y:y, width:kWindowWidth, height:kWindowHeight) + var y = (sz.height - windowHeight - (kCircleHeight / 8)) / 2 + contentView.frame = CGRect(x:x, y:y, width:kWindowWidth, height:windowHeight) y -= kCircleHeightBackground * 0.6 x = (sz.width - kCircleHeightBackground) / 2 circleBG.frame = CGRect(x:x, y:y+6, width:kCircleHeightBackground, height:kCircleHeightBackground) // Subtitle y = kTitleTop + kTitleHeight viewText.frame = CGRect(x:12, y:y, width: kWindowWidth - 24, height:kTextHeight) + viewText.frame = CGRect(x:12, y:y, width: viewTextWidth, height:viewTextHeight) // Text fields - y += kTextHeight + 14.0 + y += viewTextHeight + 14.0 for txt in inputs { txt.frame = CGRect(x:12, y:y, width:kWindowWidth - 24, height:30) txt.layer.cornerRadius = 3 - y += 40 + y += kTextFieldHeight } // Buttons for btn in buttons { btn.frame = CGRect(x:12, y:y, width:kWindowWidth - 24, height:35) btn.layer.cornerRadius = 3 - y += 45.0 + y += kButtonHeight } } @@ -203,7 +227,7 @@ public class SCLAlertView: UIViewController { public func addTextField(title:String?=nil)->UITextField { // Update view height - kWindowHeight += 40.0 + kWindowHeight += kTextFieldHeight // Add text field let txt = UITextField() txt.borderStyle = UITextBorderStyle.RoundedRect @@ -243,7 +267,7 @@ public class SCLAlertView: UIViewController { private func addButton(title:String)->SCLButton { // Update view height - kWindowHeight += 45.0 + kWindowHeight += kButtonHeight // Add button let btn = SCLButton() btn.layer.masksToBounds = true