2015-01-10 5 views
5

में बटन बनाएं, मैं सार्वभौमिक रूप से टेबलव्यू के सेक्शन हेडर में दाईं ओर एक UIButton को फ़्लोट करने का प्रयास कर रहा हूं। अब तक मैं केवल एक स्क्रीन आकार के लिए बाधाओं को जोड़ने में कामयाब रहे ...टेबलव्यू सेक्शन हेडर फ्लोट/बटन को सही प्रोग्रामेटिक // स्विफ्ट

यहाँ मेरी कोड अब तक है:

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 

} 

मैं कैसे बटन सही फ्लोट कर सकते हैं? शेष बाधाएं रह सकती हैं क्योंकि वे हैं ...

THX आपकी सहायता के लिए!

// एसईबी

+0

"फ्लोट दाएं" से आपका क्या मतलब है इसका एक उदाहरण प्रदान करेगा। आप कहते हैं कि आपने "एक स्क्रीन आकार के लिए बाधाएं जोड़ दी हैं", लेकिन आपने कोई बाधा नहीं जोड़ा है। यहां कोई कोड नहीं है जो किसी भी बाधाओं को जोड़ता है (यदि बाधाओं से, आप NSLayoutConstraints का मतलब है)। – rdelmar

+0

क्षमा करें मैं तेजी से नया हूँ! तो वास्तव में मुझे नहीं पता कि प्रोग्राम को बाधाओं को कैसे सेट किया जाए। मैं बटन को प्रत्येक डिवाइस पर सेक्शन हेडर के दाईं ओर होना चाहता हूं। 'CGRectMake (320, 10, 30, 30) के ऊपर दिए गए कोड के साथ, बटन में एक निश्चित स्थिति है जो आईफोन 6 पोर्ट्रेट पर दाईं ओर होती है। ;-) – Seb

+0

यदि आपको पता नहीं है कि ऐसा कैसे किया जाए, तो आपको NSLayoutConstraint क्लास संदर्भ पढ़ना चाहिए, जिसमें तेजी से और उद्देश्य-सी विधियों को शामिल किया जाएगा जिन्हें आपको बाधाओं को प्रोग्रामेटिक रूप से जोड़ने की आवश्यकता है। आपको कोड लिखने का प्रयास करना चाहिए, और यदि आप अटक जाते हैं तो एक और विशिष्ट प्रश्न के साथ वापस आना चाहिए। – rdelmar

उत्तर

8

Thx @rdelmar करने के लिए और यहाँ कुछ शोध जवाब है, तो किसी को भी अद्भुत जवाब @Seb साझा करने के लिए ;-)

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 interessted किया जाना चाहिए। चूंकि स्विफ्ट में कुछ बदलाव हुए हैं, जो आपके उत्तर को प्रभावित करते हैं। मैं स्विफ्ट 3 और 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 
} 
संबंधित मुद्दे