Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 758fda7

Browse files
committed
Adjust dialog sizing based on number of requests.
1 parent 9a73698 commit 758fda7

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

PermissionScope/PermissionScope.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ extension String {
9494

9595
public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGestureRecognizerDelegate {
9696
// constants
97-
let ContentWidth: CGFloat = 280.0
98-
let ContentHeight: CGFloat = 480.0
97+
let contentWidth: CGFloat = 280.0
9998

10099
// configurable things
101100
public let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
@@ -189,8 +188,10 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
189188

190189
contentView.addSubview(bodyLabel)
191190

191+
// close button
192192
closeButton.setTitle("Close", forState: UIControlState.Normal)
193193
closeButton.addTarget(self, action: Selector("cancel"), forControlEvents: UIControlEvents.TouchUpInside)
194+
closeButton.setTranslatesAutoresizingMaskIntoConstraints(false)
194195

195196
contentView.addSubview(closeButton)
196197
}
@@ -213,31 +214,42 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
213214
// Set background frame
214215
view.frame.size = screenSize
215216
// Set frames
216-
var x = (screenSize.width - ContentWidth) / 2
217-
var y = (screenSize.height - ContentHeight) / 2
218-
contentView.frame = CGRect(x:x, y:y, width:ContentWidth, height:ContentHeight)
217+
var x = (screenSize.width - contentWidth) / 2
218+
219+
let dialogHeight: CGFloat
220+
switch self.configuredPermissions.count {
221+
case 2:
222+
dialogHeight = 360
223+
case 3:
224+
dialogHeight = 460
225+
default:
226+
dialogHeight = 260
227+
}
228+
229+
var y = (screenSize.height - dialogHeight) / 2
230+
contentView.frame = CGRect(x:x, y:y, width:contentWidth, height:dialogHeight)
219231

220232
// offset the header from the content center, compensate for the content's offset
221233
headerLabel.center = contentView.center
222234
headerLabel.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
223-
headerLabel.frame.offset(dx: 0, dy: -180)
235+
headerLabel.frame.offset(dx: 0, dy: -((dialogHeight/2)-50))
224236

225237
// ... same with the body
226238
bodyLabel.center = contentView.center
227239
bodyLabel.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
228-
bodyLabel.frame.offset(dx: 0, dy: -130)
240+
bodyLabel.frame.offset(dx: 0, dy: -((dialogHeight/2)-100))
229241

230242
closeButton.center = contentView.center
231243
closeButton.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
232-
closeButton.frame.offset(dx: 105, dy: -220)
244+
closeButton.frame.offset(dx: 105, dy: -((dialogHeight/2)-20))
233245
closeButton.setTitleColor(tintColor, forState: UIControlState.Normal)
234246

235247
let baseOffset = 95
236248
var index = 0
237249
for button in permissionButtons {
238250
button.center = contentView.center
239251
button.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
240-
button.frame.offset(dx: 0, dy: -60 + CGFloat(index * baseOffset))
252+
button.frame.offset(dx: 0, dy: -((dialogHeight/2)-160) + CGFloat(index * baseOffset))
241253

242254
let type = configuredPermissions[index].type
243255

@@ -257,7 +269,7 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
257269
let label = permissionLabels[index]
258270
label.center = contentView.center
259271
label.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
260-
label.frame.offset(dx: 0, dy: -15 + CGFloat(index * baseOffset))
272+
label.frame.offset(dx: 0, dy: -((dialogHeight/2)-205) + CGFloat(index * baseOffset))
261273

262274
index++
263275
}

0 commit comments

Comments
 (0)