2015-01-10 12 views
5

Ho provato ad allineare di float un UIButton a destra nell'intestazione di sezione di un TableView universalmente. Finora ho solo riuscito a aggiungere vincoli per le dimensioni di una schermata ...crea il pulsante nell'intestazione della sezione tableview float/allinea a destra a livello di codice // Swift

Ecco il mio codice finora:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    var headerFrame:CGRect = tableView.frame 

    let titelArr: NSArray = ["1", "2", "3", "4", "5", "6"] 
    var title = UILabel(frame: CGRectMake(10, 10, 100, 30)) 
    title.font = UIFont.boldSystemFontOfSize(20.0) 
    title.text = titelArr.objectAtIndex(section) as? String 
    title.textColor = UIColor.whiteColor() 


    var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton 
    headBttn.frame = CGRectMake(320, 10, 30, 30) 
    headBttn.enabled = true 
    headBttn.titleLabel?.text = title.text 
    headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section)) 
    headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside) 

    var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height)) 
    headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9) 
    headerView.addSubview(title) 
    headerView.addSubview(headBttn) 

    return headerView 

} 

Come posso fare il tasto destro galleggiante? Il resto dei vincoli può rimanere come sono ...

THX per il vostro aiuto!

// Seb

+0

Cosa intendi per "float right"? Dici di aver "aggiunto vincoli per una dimensione dello schermo", ma non hai aggiunto alcun vincolo. Non c'è codice qui che aggiunge vincoli (se per vincoli, vuoi dire NSLayoutConstraints). – rdelmar

+0

Scusa se sono nuovo di swift! Quindi in realtà non ho idea di come impostare i vincoli a livello di codice. Voglio che il pulsante sia sul lato destro dell'intestazione della sezione su ogni dispositivo. con il codice sopra 'CGRectMake (320, 10, 30, 30)' il pulsante ha una posizione fissa che si trova sul lato destro di iPhone 6 portrait. ;-) – Seb

+0

Se non hai idea di come farlo, dovresti leggere il riferimento alla classe NSLayoutConstraint, che includerà i metodi swift e objective-C necessari per aggiungere i vincoli a livello di codice. Dovresti tentare di scrivere il codice e tornare indietro con una domanda più specifica se ti blocchi. – rdelmar

risposta

8

Thx a @rdelmar e alcune ricerche ecco la risposta se qualcuno deve essere interessavano ;-)

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    var headerFrame = tableView.frame 

    var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height)) 
    headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9) 

    var title = UILabel() 
    title.setTranslatesAutoresizingMaskIntoConstraints(false) 
    title.font = UIFont.boldSystemFontOfSize(20.0) 
    title.text = titelArr.objectAtIndex(section) as? String 
    title.textColor = UIColor.whiteColor() 
    headerView.addSubview(title) 

    var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton 
    headBttn.setTranslatesAutoresizingMaskIntoConstraints(false) 
    headBttn.enabled = true 
    headBttn.titleLabel?.text = title.text 
    headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section)) 
    headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside) 
    headerView.addSubview(headBttn) 

    var viewsDict = Dictionary <String, UIView>() 
    viewsDict["title"] = title 
    viewsDict["headBttn"] = headBttn 

    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "H:|-10-[title]-[headBttn]-15-|", options: nil, metrics: nil, views: viewsDict)) 

    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|-[title]-|", options: nil, metrics: nil, views: viewsDict)) 
    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|-[headBttn]-|", options: nil, metrics: nil, views: viewsDict)) 

    return headerView 

} 
1

Thnx per condividere la sorprendente risposta @Seb. Poiché ci sono state alcune modifiche apportate in Swift, che influisce sulla tua risposta. Fornirò un esempio di come fare lo stesso in Swift 3 e 4:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.frame.size.height)) 
    headerView.backgroundColor = UIColor.lightGray 

    let title = UILabel() 
    title.translatesAutoresizingMaskIntoConstraints = false 
    title.text = "myTitle" 
    headerView.addSubview(title) 

    let button = UIButton(type: .system) 
    button.translatesAutoresizingMaskIntoConstraints = false 
    button.setTitle("myButton", for: .normal) 
    headerView.addSubview(button) 

    var headerViews = Dictionary<String, UIView>() 
    headerViews["title"] = title 
    headerViews["button"] = button 

    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[title]-[button]-15-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[title]-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[button]-|", options: [], metrics: nil, views: headerViews)) 

    return headerView 
} 
Problemi correlati