简体   繁体   中英

UITableview Changes Y position when returned from previous view

I am having a view that contains a Navigation bar , A UISegmentedController with two segment "A" and "B". By default it has "A" selected, beside which a tableview is being displayed. Now on clicking "B" tableview will increase its y position by 20 and height is also calculated accordingly and searchBar will be displayed.

If again user select "A" then search bar will be hidden and tableview y position will be decreased by 20 and height is also calculated accordingly. It works fine when are switching between different segment. Now issue arises when user user has selected "A" segment and now If I am navigating to another view and returning back then table view's Y position is increased by 20.

My viewWillAppear doesnt contain any code, Moreover I haven't implemented any view related methods except viewWillAppear . Can anyone help me out with this?

override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")

    //Calling function that will insert static data to tableview
    self.settingStaticData()
    //Calling function that would customize Search bar
    self.customizingSearchBar()

    //For detecting whether searching is enabled or not
    boolisSearching = false
    //By setting default selected segment value to 0
    intSelectedSegmentId = 0;
    self.displayRequestview() 
}

func displayRequestview(){
    dispatch_async(dispatch_get_main_queue()) {
        self.searchBar?.hidden = true
        self.tblSuggestions?.frame = CGRectMake(0,0,(self.tblSuggestions?.frame.size.width)!,(self.tblSuggestions?.frame.size.height)!+44.0)
        print("in Request View Height \((self.tblSuggestions?.frame.size.height)!)")

    }
    //Reloading data
    self.tblSuggestions?.reloadData()
    self.tblSuggestions?.setContentOffset(CGPointZero, animated:true)
}


func displaySuggestionView(){
    dispatch_async(dispatch_get_main_queue()) {
        self.searchBar?.hidden = false
       self.tblSuggestions?.frame = CGRectMake(0,44.0,(self.tblSuggestions?.frame.size.width)!,(self.tblSuggestions?.frame.size.height)!-44.0)
        //print("After Height \(self.tblSuggestions?.frame.size.height)")
    }
    //Reloading data
    self.tblSuggestions?.reloadData()
    self.tblSuggestions?.setContentOffset(CGPointZero, animated:true)

}
@IBAction func segmentValueChanged(sender: AnyObject){

    let segmentedControl: UISegmentedControl = sender as! UISegmentedControl
    if(segmentedControl.selectedSegmentIndex == 0){
        print("Height Before Change  \((tblSuggestions?.superview?.bounds.height)!)")
        intSelectedSegmentId = 0
        displayRequestview()
    }
    else if(segmentedControl.selectedSegmentIndex == 1)
    {
        intSelectedSegmentId = 1
        displaySuggestionView()
    }
}

Go to XIB or Storyboard file

Check 'Top Bar' & 'Status Bar' settings for both views A & B.

20 points are being added/removed because of status bar.

Hope it will resolve.

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