使用swift auto layout

时间:2017-02-03 11:17:37

标签: swift uitableview autolayout

我需要将表格滚动到底部。当我在viewwilAppear中编写滚动表格的代码时,它不起作用,但是当我在viewDidAppear中编写相同的代码时,它可以工作,但用户可以看到滚动。我不想看到用户,在ViewWillAppear中滚动如何制作tableScrolling。

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)
    UIApplication.sharedApplication().delegate!.window!!.windowLevel = UIWindowLevelNormal
    self.view.backgroundColor = UIColor.whiteColor()
    viewWillAppearExecuted = true
    executeCodeOfViewViewAppear()
}

func  executeCodeOfViewViewAppear() {

    if viewWillAppearExecuted {

        viewWillAppearExecuted = false

        if appNeedsAutoResize{
            self.noLongerParticipantMessageLabel.font = UIUtils.getFontForApproprieteField(.Footnote).font
            self.typingStatusLabel.font = UIUtils.getFontForApproprieteField(.Headline).font
        }

        withAnimationFlag = true

        if !self.composeBar.textView.isFirstResponder()
        {
            // This block will execute if compose bar is first responder
            self.updateTableBottomCnt(value:self.composeBar.frame.size.height)


            // Update compose bar constarint
            if self.composeBarBottomConstraing.constant != 0
            {
                self.composeBarBottomConstraing.constant = 0
            }
        }

        self.isOpenNextScreen = false

        self.checkIfloginUserisActiveMember()

        self.tabBarController?.tabBar.hidden = true
        ChatCommonCall.sharedInstance.currentThreadIdForPush = self.threadInfo.threadId

        // This block is used for make a responder to keyboard for send message.
        if shouldComposeBarFirstResponder == true {
            self.composeBar.becomeFirstResponder()
            shouldComposeBarFirstResponder = false
        }

        self.showMessageById(self.messageIdForMessageDisplay)

        ChatCommonCall.sharedInstance.isChatModuleScreen = true
        self.changeFrame()
        self.composeBar.button.enabled = self.composeBar.text.trim().length > 0 ? true : false
        /// Table frame changes.
        switch self.optType
        {
        case .NewMessageSection:
            break
        case .DoNotHandle :
            break
        case .None :
            if !(self.messageIdForMessageDisplay.isEqual(Guid.emptyGuid()))
            {
                self.optType = .ShowMessageOfID(id: self.messageIdForMessageDisplay)
            }
            else {
                self.optType = .ScrollToBottom
            }
            break
        default :
            break
        }

        switch self.optType
        {
        case .NewMessageSection :
            break
        default :
            self.handleOptType()
        }

        //Implemented the new observer pattern in the whole project where we suppose to refresh the screen for background sync data
        //By Nikhil Kumar Saraf on 19 July 2016
        Notifier.Unregister()
        let observer = DatabaseObserverInfo(tName: ["chatthread","chatthreadmember","chatmessage","chatmessagereceiver","chatthumbnail","pfthumbnail","chatmessagemarker"], oType: [SyncOpType.Insert,SyncOpType.Update,SyncOpType.Delete], observer: self, stick: false, syncTrigger: SyncTriggerType.Post)
        Notifier.Register(observer)


        //By Balkrishan Yadav for AutoResizing UI on 30 Nov. 2016
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChatMessageController.didChangePreferredContentSize(_:)), name:UIContentSizeCategoryDidChangeNotification, object: nil)

        // This  condition will execute if some one sharing image and coming to the chat message screen
        //@Balkrishan Yadav
        if ChatCommonCall.sharedInstance.attachmentInfo != nil {
            self.addFileInComposeBar(ChatCommonCall.sharedInstance.attachmentInfo!)
            ChatCommonCall.sharedInstance.attachmentInfo = nil
            let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
            dispatch_after(dispatchTime, dispatch_get_main_queue(), {
                // your function here
               // self.changeController(true)
            })
        }
    }
}
 func handleOptType(callFromViewDidAppear:Bool = false) {
        switch optType {
        case .ScrollToBottom : // No new message is available. show last message.         self.messageTableView.setContentOffset(CGPointMake(0,CGFloat.max), animated: !isCameFromThreadComposer)
            break
      default:
    // do nothing
    }

1 个答案:

答案 0 :(得分:0)

如果您不希望动画滚动,则只需将false作为animated参数传递给滚动功能。例如:

tableView.scrollToRow(at: indexPath, at: .top, animated: false)

此函数会将indexPath的行滚动到表格视图的顶部而不进行任何动画。