简体   繁体   中英

change button position in scrollview

I have ScrollView and in this scroll view I have buttons and one table. I want to change position buttons and size table when my form was loaded. For example, if the table is made up of 5 cells, buttons position need to change on 200 by y (down). And table need to has height = height_table + 200.

My code:

var yPosition: CGFloat = 0;
var scrollViewContentSize: CGFloat = 505;
var names = ["first","second","third","fourth","fifth"]

 override func viewDidAppear(animated: Bool) {

    self.yPosition += 40 * CGFloat(names.count)
    self.scrollViewContentSize += 40 * CGFloat(names.count)
    self.scrlMain.contentSize = CGSize(width: 320,height: self.scrollViewContentSize)

    let btnY1: CGFloat = 40 * CGFloat(names.count) + 467
    btn1.frame.origin = CGPoint(x: 210, y: btnY1)

    let btnY2: CGFloat = 44 * CGFloat(names.count) + 386
    btn2.frame = CGRectMake(26, btnY2, 46, 30)

    let btnY3: CGFloat = 44 * CGFloat(names.count) + 427
    btn3.frame = CGRectMake(26, btnY3, 46, 30)

    var framelb: CGRect = lb.frame
    framelb.origin.y += 40 * CGFloat(names.count)
    framelb.origin.x = 110
    lb.frame = framelb

    tableView.contentOffset.y += 40 * CGFloat(names.count)
    tableView.contentSize = CGSize(width: 304, height: tableView.contentOffset.y)
}

What did I do wrong?

screen1

screen2

I tried to add func setControlsPosition, but nothing happened.

Code:

func setControlsPosition(){
tableView.contentOffset.y += 40 * CGFloat(names.count)
tableView.contentSize = CGSize(width: 304, height: tableView.contentOffset.y)

let lblY: CGFloat = tableView.frame.origin.y + tableView.frame.size.height+10 //10 for spacing between tableView and label
lb.frame = CGRectMake(26, lblY, 46, 30)

let btn1Y: CGFloat = lb.frame.origin.y + lb.frame.size.height + 10 // Spacing between label and button
btn1Y.frame = CGRectMake(26, btn1Y, 46, 30)
}

//This is code work well
   override func viewDidAppear(animated: Bool) {

    self.yPosition += 40 * CGFloat(names.count)
    self.scrollViewContentSize += 40 * CGFloat(names.count)
    self.scrlMain.contentSize = CGSize(width: 320,height: self.scrollViewContentSize)
//This function is performed, but nothing changes
    setControlsPosition()
}

I checked, names.count = 5 in the function setControlsPosition. It is correct.

You can use the previous control as a reference to define the position other control

override func viewDidAppear(animated: Bool) {

    setControlsPosition()

    self.scrollViewContentSize = button3.frame.origin.y + button3.frame.size.height + 10
    self.scrlMain.contentSize = CGSize(width: 320,height: self.scrollViewContentSize)
}

func setControlsPosition(){
    tableView.contentOffset.y += 40 * CGFloat(names.count)
    tableView.contentSize = CGSize(width: 304, height: tableView.contentOffset.y)

    let lblY: CGFloat = tableView.frame.origin.y + tableView.frame.size.height+10 //10 for spacing between tableView and label
    lb.frame = CGRectMake(26, lblY, 46, 30)

    let btn1Y: CGFloat = lb.frame.origin.y + lb.frame.size.height + 10 // Spacing between label and button
    btn1Y.frame = CGRectMake(26, btn1Y, 46, 30)
}

Please understand and then implement.

Similarly you can do for all you'r other controls. After setting all other controls you can define the scroll contentsize as per the last control's position.

All this thing can be more simpler with Autolayout. Try to use that.

Hope this may help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM