2015-01-10 17 views
5

Tôi cố gắng căn chỉnh float một UIButton ở bên phải trong phần tiêu đề của một TableView phổ quát. Cho đến nay tôi chỉ cố gắng thêm trở ngại cho một cỡ màn hình ...thực hiện nút trong tiêu đề phần tableview float/align right programmatically // Swift

Đây là mã của tôi cho đến nay:

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 

} 

Làm thế nào tôi có thể làm cho các nút nổi phải không? Phần còn lại của các ràng buộc có thể giữ nguyên như ...

THX để được trợ giúp của bạn!

// Seb

+0

Ý của bạn là gì bằng "float right"? Bạn nói rằng bạn đã "thêm ràng buộc cho một kích thước màn hình", nhưng bạn chưa thêm bất kỳ ràng buộc nào cả. Không có mã nào ở đây thêm bất kỳ ràng buộc nào (nếu do ràng buộc, bạn có nghĩa là NSLayoutConstraints). – rdelmar

+0

Xin lỗi tôi mới đến nhanh! Vì vậy, thực sự tôi không có ý tưởng làm thế nào để thiết lập ràng buộc lập trình. Tôi muốn nút ở bên phải của tiêu đề phần trên mọi thiết bị. với mã trên 'CGRectMake (320, 10, 30, 30)' nút có vị trí cố định xảy ra ở bên phải trên iPhone 6 chân dung. ;-) – Seb

+0

Nếu bạn không có ý tưởng làm thế nào để làm điều đó, sau đó bạn nên đọc các tài liệu tham khảo lớp NSLayoutConstraint, trong đó sẽ bao gồm các phương pháp nhanh chóng và khách quan-C bạn cần phải thêm các ràng buộc lập trình. Bạn nên cố gắng viết mã và quay lại với một câu hỏi cụ thể hơn nếu bạn gặp khó khăn. – rdelmar

Trả lời

8

Thx để @rdelmar và một số nghiên cứu ở đây là câu trả lời nếu ai nên interessted ;-)

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 để chia sẻ câu trả lời @Seb tuyệt vời. Vì đã có một số thay đổi được thực hiện trong Swift, điều này ảnh hưởng đến câu trả lời của bạn. Tôi sẽ cung cấp ví dụ về cách thực hiện tương tự trong Swift 3 và 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 
} 
Các vấn đề liên quan