Display a well positioned Horizontal and Vertical dynamically multiple objects applicable to all devices
for example here any object as "button". Follow my code.
Here i will be displaying an array for numbers as titles for buttons and it can be highlighted by clicking on it
class arrayStatus: NSObject {
let title: String
let status: String
init(title: String, status: String) {
self.title = title
self.status = status
}
}
class customViewController: ViewController{
var ageArray = [arrayStatus]()
let customView = UIScrollView()
override func viewDidLoad() {
/* Here i am appending 100 numbers as stings in to an array and just for default selection i changed the status one string */
/* Here i have taken a customView as UIScrollView and i will be display the dynamic objects in it and i just added some styles to it */
/* I have created and called a custom function for displaying the objects */
for var i = 1; i <= 100; ++i{
var ageStatus = "off"
if(i == 24){
ageStatus = "on"
}
self.ageArray.append(arrayStatus(title: String(i), status: ageStatus))
}
customView.frame = CGRectMake(15, 40, self.view.frame.width - 30, 50)
self.view.addSubview(customView)
customView.backgroundColor = UIColor.whiteColor()
customView.layer.cornerRadius = 4
customView.layer.borderColor = UIColor.darkGrayColor().CGColor
customView.layer.borderWidth = 1
self.displayObjects(customView)
}
func displayObjects(tView:UIScrollView){
var pos_x: CGFloat = 10
var pos_y: CGFloat = 10
for var i = 0; i < self.ageArray.count; ++i{
let selectAgeButton = UIButton()
selectAgeButton.frame = CGRectMake(pos_x, pos_y, 30, 30)
selectAgeButton.setTitle(self.ageArray[i].title, forState: .Normal)
selectAgeButton.setTitleColor(UIColor.greenColor(), forState: .Normal)
selectAgeButton.layer.borderWidth = 1
selectAgeButton.layer.borderColor = UIColor.greenColor.CGColor
selectAgeButton.layer.cornerRadius = 5
selectAgeButton.layer.masksToBounds = true
selectAgeButton.addTarget(self, action: "selectAgeButtonAction:", forControlEvents: .TouchUpInside)
let labWidth = selectAgeButton.frame.origin.x + selectAgeButton.frame.width
let pViewWidth = tView.frame.width
if(labWidth > pViewWidth){
pos_x = 10
pos_y = pos_y + selectAgeButton.frame.height + 10
selectAgeButton.frame.origin.x = pos_x
selectAgeButton.frame.origin.y = pos_y
pos_x = pos_x + selectAgeButton.frame.width + 15
}
else{
selectAgeButton.frame.origin.x = pos_x
selectAgeButton.frame.origin.y = pos_y
pos_x = pos_x + selectAgeButton.frame.width + 15
pos_y = selectAgeButton.frame.origin.y
}
if(self.ageArray[i].status == "on"){
selectAgeButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
selectAgeButton.backgroundColor = UIColor.greenColor()
}
tView.frame.size.height = selectAgeButton.frame.origin.y + selectAgeButton.frame.height + 10
if(tView.frame.size.height > 215){
tView.frame.size.height = 215
tView.contentSize.height = selectAgeButton.frame.origin.y + selectAgeButton.frame.height + 50
}
tView.addSubview(selectAgeButton)
}
}
func selectAgeButtonAction(button:UIButton){
for sview in (button.superview?.subviews)!{
(sview as? UIButton)?.backgroundColor = UIColor.whiteColor()
(sview as? UIButton)?.setTitleColor(UIColor(netHex:0x0c8506), forState: .Normal)
}
(button as? UIButton)?.backgroundColor = UIColor(netHex:0x0c8506)
(button as? UIButton)?.setTitleColor(UIColor.whiteColor(), forState: .Normal)
}
}

No comments:
Post a Comment