2017-02-19 3 views
1

UITableViewヘッダーセクションにカスタム画像を追加しました。画面がポートレートモードのときはうまくいきます。ランドスケープモードでは大きなギャップが現れます。誰も私を助けることができますか? AutoLayoutの問題UITableビューヘッダーセクション:左の画像を追加するとうまく動作しません。

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    var headerView = UIView(frame: CGRect(x: 1, y: 1, width: tableView.frame.width, height: 40)) 
    var myLabel = UILabel() 
    myLabel.frame = CGRectMake(0, 0, tableView.frame.width - 70, 40) 
    print(myLabel.frame) 
    myLabel.font = UIFont.boldSystemFontOfSize(18) 
    myLabel.backgroundColor = UIColor.blueColor() 
    myLabel.text = self.tableView(tableView, titleForHeaderInSection: section) 
    let button = UIButton(frame: CGRect(x: 230,y: 0,width: 100,height: 40)) 
    button.tag = section 
    button.backgroundColor = UIColor.clearColor() 
    headerView.addSubview(button) 
    headerView.addSubview(myLabel) 
    headerView.backgroundColor = UIColor.clearColor() 

    // the button is image - set image 
    button.setImage(UIImage(named: "icoDraft"), forState: UIControlState.Normal) 
    let tapOnCardCell: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(HHLabTestExaminationViewController.handleTapOnSectionImage(_:))) 
    button.addGestureRecognizer(tapOnCardCell) 

    return headerView 
} 

次にヘッダ部のタイトル画面が縦向きにあるとき、この画像は私の出力である

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if(section == 0) 
    { 
     return "Exam" 
    } 
    else if(section == 1) 
    { 
     return "News" 
    } 
    else if(section == 2) 
    { 
     return "Movie" 
    } 
    else if(section == 3) 
    { 
     return "Sport" 
    } 
    return "" 
} 

ある: enter image description here

画面が横向きであるときです。どのように私は景観のこのギャップを修正することができますか? enter image description here

答えて

2

このヘッダービューを設定すると、自動レイアウトコードが表示されません。しかし、この単純なレイアウトは自動レイアウトなしで処理できます。

headerView.autoResizesSubviews = true 
myLabel.autoResizingMask = [.flexibleWidth, .flexibleHeight] 
button.autoResizingMask = .flexibleLeftMargin 
+0

ありがとうございました。 – Sam

1

@DaveWestonによると、自動レイアウトコードはありません。彼の答えはうまくいくはずです。自動レイアウトが必要な場合、ボタンの外観は次のようになります。

(これはOPのコードではSwift 3と2.xです)。

let button = UIButton() 
    button.tag = section 
    button.backgroundColor = UIColor.clear 
    headerView.addSubview(button) 

    // Autolayout for button 
    button.translatesAutoresizingMaskIntoConstraints = false 
    button.addConstraint(NSLayoutConstraint.init(item: button, 
               attribute: .height, 
               relatedBy: .equal, 
               toItem: headerView, 
               attribute: .height, 
               multiplier: 1.0, 
               constant: 0.0)) 
    button.addConstraint(NSLayoutConstraint.init(item: button, 
               attribute: .width, 
               relatedBy: .equal, 
               toItem: nil, 
               attribute: .width, 
               multiplier: 1.0, 
               constant: 40.0)) 
    button.addConstraint(NSLayoutConstraint.init(item: button, 
               attribute: .trailing, 
               relatedBy: .equal, 
               toItem: headerView, 
               attribute: .trailing, 
               multiplier: 1.0, 
               constant: 0.0)) 
    button.addConstraint(NSLayoutConstraint.init(item: button, 
               attribute: .centerY, 
               relatedBy: .equal, 
               toItem: headerView, 
               attribute: .centerY, 
               multiplier: 1.0, 
               constant: 0.0)) 
+0

その本当に素晴らしいソリューションありがとう:) :)。これも私のために働いた – Sam

関連する問題