2016-09-19 20 views
9

पर UIScrollView के शीर्ष पर गैप मुझे UIScrollView पर बाधाओं के साथ कोई समस्या है जो आईओएस 10-विशिष्ट प्रतीत होता है। मुझे स्क्रॉल व्यू के शीर्ष और अंदर के सामग्री दृश्य के बीच एक अंतर दिखाई देता है, जिसे शीर्ष पर अटक जाना चाहिए।आईओएस 10

आईओएस 9 पर कोई अंतर नहीं दिखता है, लेकिन आईओएस 10 पर अंतर दिखाई देता है।
स्पष्ट होने के लिए, दोनों स्थितियों में स्क्रॉल व्यू टॉप शीर्ष लेआउट मार्गदर्शिका के नीचे पिन किया गया है, जो नेविगेशन बार के नीचे से पूरी तरह से लाइनों के साथ लाइनें हैं। आईओएस 10 स्क्रॉल व्यू के शीर्ष और सामग्री दृश्य के शीर्ष के बीच नेविगेशन बार का आकार एक अंतर पेश करता है।

मैं शीर्ष लेआउट मार्गदर्शिका के शीर्ष पर स्क्रॉल व्यू के शीर्ष को संरेखित कर सकता हूं, जो नेविगेशन बार के नीचे का अंतर रखेगा और सामग्री दृश्य ठीक हो जाएगा, लेकिन आईओएस 9 पर सामग्री दृश्य नीचे होगा नेविगेशन बार जो अवांछित है।

मैंने जल्दी से कुछ खेल का मैदान कोड बनाया है जो नीचे दिए गए मुद्दे को प्रदर्शित करता है। क्या मुझे कुछ याद आ रही है? यह एक मुद्दा बनाने के लिए आईओएस 10 में क्या बदल गया, और मैं इसके आसपास कैसे काम करूं?

comparison of screenshots from iPhone 5s simulator

import UIKit 
import PlaygroundSupport 

class TestViewController: UIViewController { 
    var mainScrollView: UIScrollView 
    var contentView: UIView 

    init() { 
     self.mainScrollView = UIScrollView() 
     self.contentView = UIView() 

     super.init(nibName: nil, bundle: nil) 

     self.view.backgroundColor = UIColor.white 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func viewDidLoad() { 
     self.mainScrollView.backgroundColor = UIColor.green 
     self.contentView.backgroundColor = UIColor.blue 

     self.mainScrollView.translatesAutoresizingMaskIntoConstraints = false 
     self.contentView.translatesAutoresizingMaskIntoConstraints = false 

     self.view.addSubview(self.mainScrollView) 
     self.mainScrollView.addSubview(self.contentView) 

     // constrain the scroll view bounds to the view 
     self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.topLayoutGuide, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)) 
     self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)) 
     self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)) 
     self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)) 

     // constrain the content view bounds to the scroll view 
     self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)) 
     self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)) 
     self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)) 
     self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)) 

     // constrain the content view's size to the view's size 
     self.view.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)) 
     self.view.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: self.view, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0)) 
    } 
} 

let rootViewController = TestViewController() 
rootViewController.title = "Test" 

let navigationController = UINavigationController(rootViewController: rootViewController) 
PlaygroundPage.current.liveView = navigationController.view 
+0

मेरे पास एक ही समस्या है ... केवल आईओएस 10 में होती है। क्या आपके पास इसे तय करने में कोई भाग्य है? – inorganik

+0

@inorganik - नहीं, अभी तक नहीं। प्रोजेक्ट के साथ आगे बढ़ने की तरह बस इसे छोड़ दें। एक ओएस संस्करण जांच में डाल दिया जा सकता है, जिसे मैं टालने की उम्मीद कर रहा हूं। –

+0

मैंने जो समाधान पाया है, उसे जोड़ा गया है, उम्मीद है कि यह आपके लिए काम करता है। – inorganik

उत्तर

1

पहले बाधा में आप आप topLayoutGuidebottom के लिए सेट की स्थापना कर रहे हैं। तो topLayoutGuide के bottom से ऐप्पल documentationbottom से आपके सबव्यूज़ को ढेर करने के तरीके पर निर्भर करता है। तो आपके उदाहरण में यदि आप इसे

self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .top, multiplier: 1, constant: 0)) पर सेट करते हैं तो यह काम करता है।

लेकिन मैं नियमित रूप से अपने सबव्यूव के शीर्ष का उपयोग करता हूं ताकि मेरे पास "मार्जिन" न हो। तो यह इस तरह दिख सकता है: self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0))

उम्मीद है कि यह मदद करता है।

+0

मैं अपने कंप्यूटर से दूर हूं इसलिए मैं दोबारा जांच नहीं कर सकता पल, लेकिन मुझे पूरा यकीन है कि अगर मैं इसे शीर्ष लेआउट गाइड के शीर्ष पर सेट करता हूं तो यह वास्तव में आईओएस 10 पर काम करता है, लेकिन आईओएस 9 पर यह नेविगेशन बार के पीछे समाप्त होता है, जिसे मैं टालने की कोशिश कर रहा हूं, जो कि मैं पर्यवेक्षण के शीर्ष पर इसे संरेखित करने के साथ क्यों नहीं गया था। –

+0

बस इसमें जोड़ने के लिए, स्क्रॉल व्यू का शीर्ष उस स्थान पर है जहां मैं इसे आईओएस 9 और आईओएस 10 दोनों पर होना चाहता हूं: नेविगेशन बार के नीचे से लाइन में। मुद्दा स्क्रॉल व्यू के शीर्ष और सामग्री दृश्य के शीर्ष के बीच का अंतर है।यदि मैं स्क्रॉल व्यू को शीर्ष लेआउट मार्गदर्शिका के शीर्ष पर ले जाता हूं, तो आईओएस 10 पर स्क्रॉल व्यू का शीर्ष नेविगेशन बार के नीचे है और सामग्री दृश्य नेविगेशन बार के अनुरूप है। आईओएस 9 पर, सामग्री दृश्य नेविगेशन बार के नीचे भी है। –

0

मुझे एक ही समस्या थी, मेरे मामले में UISearchController के लिए, जहां दृश्य को जोड़ना गतिशील रूप से किया गया था, इसलिए मुझे बाधाओं को जोड़ने का अवसर नहीं होगा। मुझे बस स्क्रॉल व्यू के contentInset को मैन्युअल रूप से सेट करना पड़ा (जो मेरे मामले में एक तालिका दृश्य था) और इसे इच्छित स्क्रीन के शीर्ष से दूरी पर सेट करें। आपके लिए यह ऐसा दिखाई दे सकता है:

// status bar + nav bar = 64 
self.mainScrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0); 
self.view.automaticallyAdjustsScrollViewInsets = NO; 

कृपया ध्यान दें कि मैंने इसे अपने ओब्ज से तेज़ी से अनुकूलित करने के लिए अनुकूलित किया है। यदि कोड थोड़ा सा है तो सी कोड क्षमा करें। मैंने आईओएस 9 और 10 में परीक्षण किया और यह दोनों में सही लग रहा था।