Showing posts with label storyboard. Show all posts
Showing posts with label storyboard. Show all posts

Tuesday, October 31, 2017

Flexdashboard/plotly interaction results in odd scroll bar behavior

Leave a Comment

I have a bizarre and very frustrating problem. When I build plotly graphs within storyboards (from the flexdashboard package), I get a very annoying and totally unnecessary scroll bar in my legend. When someone tries to click one of the dots on or off, the scroll bar twitches and its practically impossible to click the thing. This scroll bar only appears when the tab with the plotly graph is not immediately visible during the load of the page - i.e. if the page loads with some other tab selected.

I can make the same graph outside of the storyboard, with no problems either in RStudio, or saving it as an htmlwidget and loading it in Chrome. But when I load my storyboard, either in RStudio or in Chrome, I get this annoying scrollbar. The scrollbar exists whether it's a vertical or horizontal legend.

ggplotly objects do not have this problem.

Here's an example of the unnecessary scroll bar. The ggplotly graph is fine, and the plotly one has the scrollbar.

--- title: "Untitled" output:    flexdashboard::flex_dashboard:     storyboard: true ---  ```{r setup, include=FALSE} library(flexdashboard) ```  ### ggplot  ```{r}  library(plotly)  carggplot <- ggplot(mtcars, aes(hp, mpg, fill = as.factor(carb))) +     geom_point() +     theme_bw()  ggplotly(carggplot) ```   ### plotly  ```{r} carsplot <- plot_ly(     data = mtcars,     x = ~hp,     y = ~mpg,     color = ~as.factor(carb),     type = "scatter",     mode = "markers"     )  carsplot ``` 

I have been unable to find any documentation on this issue, although I found a similar problem posted by someone using the python interface to plotly.

I'm looking for a way to either turn off the scroll bar completely (while keeping the legend), or some explanation of the scroll bar's twitchy behavior.

flexdashboard is 0.5, plotly is 4.7.1, R is 64 bit 3.4.1, Windows 7.

1 Answers

Answers 1

There are quite a lot of moving parts going on to answer your question. I won't cover each in detail but touch on them only briefly.

Background

  1. The plotly.js chart is behaving as designed when the length of the legend gets longer, it automatically inserts the scrollbar.

  2. This is not happening with the ggplotly chart because all the visual styling is coming from the ggplot object.

  3. flexdashboard is the culprit in this case because of how it is dynamically fitting the available space and informing plotly.js on how to render. It is worth noting this appears as SVG in the source html.

  4. The solution is then to manipulate the DOM in order to hide/remove/alter the problematic element.

Potential Solution

The solution I offer below is therefore a bit of a hack. A more permanent solution may be to lodge an issue with the good people at RStudio to see if any change could be made to the package, which addresses your problem.

If you add runtime: shiny to your YAML header, you can then make use of the excellent shinyjs package by Dean Attali. While I'm not an expert in this space, I've added a few lines to your MRE that remove <rect> elements from the SVG of class=scrollbar. Important to note you may need to alter the javascript I offer to be more specific and not remove elements you may wish to retain.

Update to MRE

Here is the Rmd code with comments where I've made changes.

--- title: "Untitled" output:    flexdashboard::flex_dashboard:     storyboard: true   runtime: shiny ---  ```{r setup, include=FALSE} library(shinyjs)        # add package, note runtime option above in YAML useShinyjs(rmd = TRUE)  # function needs to initialise js  library(flexdashboard) library(plotly) ```  ### ggplot  ```{r}  library(plotly)  carggplot <- ggplot(mtcars, aes(hp, mpg, fill = as.factor(carb))) +     geom_point() +     theme_bw()  ggplotly(carggplot) ```   ### plotly  ```{r} carsplot <- plot_ly(     data = mtcars,     x = ~hp,     y = ~mpg,     color = ~as.factor(carb),     type = "scatter",     mode = "markers"     )  carsplot  runjs("$svg.selectAll('rect[class=scrollbar]').remove();") # run plain js to remove elements ``` 

N.B. As I post this, I have had unreliable results and will dig in further.

Read More

Sunday, August 13, 2017

how to use Objective-C project in my Swift project

Leave a Comment

Note: I know How to call Objective-C code from Swift, but I don't know below,

I'm new to ios. I want to use this EsptouchForIOS's Demo in my project. The demo is write in OC, it has a storyboard and controller. I want to know how to integrate the demo in my swift project, and use that storyboard and it's controller in my swift project.

3 Answers

Answers 1

I'll start writing from the very beginning. Suppose you have a project in Objective-C and now you want to continue your project's development in Swift. Follow the below guidelines: (This intends to your specific needs)

First choose to add a new file from File->New->File. In this process select your language as Swift. In the final step here, you will be prompted to Create Bridging Header. Select that:

Bridging Header creation

Now build your project once (⌘+B). You may get an error like this:

iOS SDK version error message

Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot) Changing deployment target

To use Objective-C resources in Swift files:

Now that you've got one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you just include the header file of that class in this bridging header file. Like in this project, you have ESP_NetUtil and ESPViewController class and their header files too. You want to expose them to Swift and use them later in Swift code. So import them in this bridging header file:

Importing Objective-C header files in Bridging Header

Build once again. Now you can go to your Swift file. And use the Objective-C classes as like you use any resource in swift. See:

Objective-C classes used in Swift

N.B: You must expose all the class headers (that you're intending to use later in Swift) in that bridging header file

To use Swift resources in Objective-C files:

Now you may wonder, I've successfully used Objective-C resources in Swift. What about the opposite? Yes! You can do the opposite too. Find your Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name. This is the header file you will be using inside your Objective-C classes for any Swift to Objective-C interoperability. To know more check here.

Objective-C Generated Interface Header Name

Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:

Import and using Swift resource in Objective-C

You will get more understanding from the official apple documentation.

You can checkout the worked out version of your linked project here with Objective-C-Swift interoperability.

Answers 2

So according to your question, you have added an objective C bridge in your swift project using How to call Objective-C code from Swift.

Now, import all headers (.h) files of your objective-c source code (demo project) that you want to direct use in swift file.

For example, your demo project has EsptouchForIOS following header (file with extension .h) files in project source code.

ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h

import a header file in your bridge, which you want to use in your swift code. Suppose in your swift code you want touch delegate ESPTouchDelegate then write,

#import "ESPTouchDelegate.h" 

Here is snapshot of your demo integration in my Test Swift project with bridge

enter image description here

and import statements.

enter image description here

Now, there is function/method in an objective C file getValue

enter image description here

which is used/accessed in swift project/file.

enter image description here

Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.

Answers 3

I have never tried to use objective-c from swift project. But I normally used swift classes from my objective-c project. I usually follow this instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from apple developer website.

Read More

Friday, March 10, 2017

Resize table header when using dynamic types

Leave a Comment

I have a TableViewController with a header. This header is a container, which links to another storyboard named Header.storyboard

TableViewController

Header.storyboard contains a stack view with some labels, that are dynamically typed.

Labels text comes from DB.

No regards to the text dimension or size I want to visualise it correctly.

enter image description here

I've used some answers from SO to get the header correctly resize, but without luck:

import UIKit  class TableViewController: UITableViewController {      @IBOutlet weak var tableHeaderView: UIView!      override func viewDidLayoutSubviews() {         super.viewDidLayoutSubviews()          // Dynamic sizing for the header view         if let headerView = tableHeaderView {             headerView.setNeedsLayout()             headerView.layoutIfNeeded()              let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height             var headerFrame = headerView.frame              // If we don't have this check, viewDidLayoutSubviews() will get             // repeatedly, causing the app to hang.             if height != headerFrame.size.height {                 headerFrame.size.height = height                 headerView.frame = headerFrame                 tableHeaderView = headerView            }        }    } } 

3 Answers

Answers 1

try to implement

tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)  

and

tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)
your code should look like this :

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {     return tableHeaderView.bounds.size.height }  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {     return tableHeaderView } 

Answers 2

Table view header can be automatically resized

but with containers it likely will not work

use custom view instead for viewForHeaderInSection

class ViewController: UITableViewController {     override func viewDidLoad() {         tableView.estimatedSectionHeaderHeight = 10         tableView.sectionHeaderHeight = UITableViewAutomaticDimension     }     override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {         let lab = UILabel()         lab.text = "text \(section)"         lab.font = UIFont.systemFont(ofSize: 10 * CGFloat(section) + 1)         return lab     }     override func numberOfSections(in tableView: UITableView) -> Int {         return 5     }     //this method overriding is not necessary     override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {         return UITableViewAutomaticDimension     } } 

Result

Answers 3

First, instead of giving the height to the view, you should give the label a height

class ViewController : UITableViewController {  override func viewDidLoad() {     super.viewDidLoad()     // Do any additional setup after loading the view, typically from a nib. }  override func didReceiveMemoryWarning() {     super.didReceiveMemoryWarning()     // Dispose of any resources that can be recreated. }  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {     return 40 }  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return 4 }  override func numberOfSections(in tableView: UITableView) -> Int {     return 1 }  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)      cell.backgroundColor = UIColor.yellow     cell.textLabel?.text = "Baran"      return cell  }  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {     return heightForView() }  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {     let headerView : UIView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.heightForView()))     headerView.backgroundColor = UIColor.black      let label : UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.heightForView()))     label.numberOfLines = 0     label.textAlignment = NSTextAlignment.center     label.text = "Size To Fit Tutorial"     label.font = UIFont(name: "Helvetica", size: 50)     label.textColor = UIColor.white     headerView.addSubview(label)      return headerView }  //Self Sizing height .... func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat{     let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))     label.numberOfLines = 0     label.lineBreakMode = NSLineBreakMode.byCharWrapping     label.font = font     label.text = text     label.sizeToFit()     return label.frame.height }  func heightForView() -> CGFloat{      let screenSize = UIScreen.main.bounds     let screenWidth = screenSize.width      let text = "Size To Fit Tutorial"     let font : UIFont!     switch UIDevice.current.userInterfaceIdiom {     case .pad:         font = UIFont(name: "Helvetica", size: 35)     case .phone:         font = UIFont(name: "Helvetica", size: 50)     default:         font = UIFont(name: "Helvetica", size: 24)     }     let height = heightForLabel(text: text, font: font, width: screenWidth)      return height } } 

enter image description here

Hope this helps

Read More

Wednesday, February 15, 2017

iOS: Default IB popover transition does strange things

Leave a Comment

I bumped into a strange issue where when a view controller is presented in a popover, the presenting view controllers view is not snapped to the bottom edge of it's superview, in that case the window itself.

AppDelegate code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     // Override point for customization after application launch.     let window = UIWindow(frame: UIScreen.main.bounds)     window.rootViewController = UIStoryboard(name: "NavigationController", bundle: Bundle.main).instantiateInitialViewController()     window.makeKeyAndVisible()     window.backgroundColor = .yellow // Needed to detect the issue.     self.window = window      return true } 

Storyboard to test:

<?xml version="1.0" encoding="UTF-8"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="4lt-Df-IeL">     <device id="retina5_5" orientation="portrait">         <adaptation id="fullscreen"/>     </device>     <dependencies>         <deployment identifier="iOS"/>         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>     </dependencies>     <scenes>         <!--Navigation Controller (locked)-->         <scene sceneID="ryr-GW-wBz">             <objects>                 <viewController storyboardIdentifier="NavigationController" definesPresentationContext="YES" id="4lt-Df-IeL" userLabel="Navigation Controller (locked)" sceneMemberID="viewController">                     <layoutGuides>                         <viewControllerLayoutGuide type="top" id="gxR-oc-l6J"/>                         <viewControllerLayoutGuide type="bottom" id="Py7-tH-kHP"/>                     </layoutGuides>                     <view key="view" contentMode="scaleToFill" id="865-DH-bRZ" userLabel="Background View">                         <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>                         <subviews>                             <containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="535-Cq-y2B">                                 <rect key="frame" x="54" y="64" width="360" height="618"/>                                 <connections>                                     <segue destination="G7K-pY-ZSi" kind="embed" identifier="ContainerViewController" id="0Mw-mh-5NT"/>                                 </connections>                             </containerView>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="xLz-jb-9OM" userLabel="Left View">                                 <rect key="frame" x="0.0" y="64" width="54" height="672"/>                                 <subviews>                                     <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="bls-BC-WIu" userLabel="Left Stack View">                                         <rect key="frame" x="0.0" y="0.0" width="54" height="672"/>                                     </stackView>                                     <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Zbx-7u-5oe" userLabel="Hairline View">                                         <rect key="frame" x="53" y="0.0" width="1" height="672"/>                                         <color key="backgroundColor" red="0.43079092749999998" green="0.1140047376" blue="0.1180379456" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                         <constraints>                                             <constraint firstAttribute="width" constant="1" id="kna-vk-GlF" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                         </constraints>                                     </view>                                 </subviews>                                 <color key="backgroundColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <constraints>                                     <constraint firstAttribute="width" constant="54" id="2bA-Ap-aup"/>                                     <constraint firstItem="bls-BC-WIu" firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="leading" id="3sM-Bd-eEq"/>                                     <constraint firstAttribute="bottom" secondItem="Zbx-7u-5oe" secondAttribute="bottom" id="9PW-0y-kBv"/>                                     <constraint firstAttribute="trailing" secondItem="Zbx-7u-5oe" secondAttribute="trailing" id="RlN-G4-8iD"/>                                     <constraint firstAttribute="trailing" secondItem="bls-BC-WIu" secondAttribute="trailing" id="anE-HT-oQp"/>                                     <constraint firstItem="Zbx-7u-5oe" firstAttribute="top" secondItem="xLz-jb-9OM" secondAttribute="top" id="bxG-xS-wdV"/>                                     <constraint firstItem="bls-BC-WIu" firstAttribute="top" secondItem="xLz-jb-9OM" secondAttribute="top" id="ds3-z5-Dpv"/>                                     <constraint firstAttribute="bottom" secondItem="bls-BC-WIu" secondAttribute="bottom" id="f9r-FV-enN"/>                                 </constraints>                             </view>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="V3o-XH-Xkn" userLabel="Bottom View">                                 <rect key="frame" x="0.0" y="682" width="414" height="54"/>                                 <subviews>                                     <stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="fM3-hA-kGa" userLabel="Bottom Stack View">                                         <rect key="frame" x="0.0" y="0.0" width="414" height="54"/>                                     </stackView>                                     <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Gtr-bO-frP" userLabel="Hairline View">                                         <rect key="frame" x="0.0" y="0.0" width="414" height="1"/>                                         <color key="backgroundColor" red="0.43079092749999998" green="0.1140047376" blue="0.1180379456" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                         <constraints>                                             <constraint firstAttribute="height" constant="1" id="K5f-m2-BpE" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                         </constraints>                                     </view>                                 </subviews>                                 <color key="backgroundColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <constraints>                                     <constraint firstItem="Gtr-bO-frP" firstAttribute="leading" secondItem="V3o-XH-Xkn" secondAttribute="leading" id="9fX-NI-RdG"/>                                     <constraint firstAttribute="bottom" secondItem="fM3-hA-kGa" secondAttribute="bottom" id="KxS-jW-8Rh"/>                                     <constraint firstItem="fM3-hA-kGa" firstAttribute="top" secondItem="V3o-XH-Xkn" secondAttribute="top" id="VLk-U3-Dgt"/>                                     <constraint firstAttribute="trailing" secondItem="Gtr-bO-frP" secondAttribute="trailing" id="aDz-aj-8ah"/>                                     <constraint firstAttribute="trailing" secondItem="fM3-hA-kGa" secondAttribute="trailing" id="bzw-Bv-nvH"/>                                     <constraint firstAttribute="height" constant="54" id="ghd-qS-t3l"/>                                     <constraint firstItem="Gtr-bO-frP" firstAttribute="top" secondItem="V3o-XH-Xkn" secondAttribute="top" id="nZz-Tn-i7X"/>                                     <constraint firstItem="fM3-hA-kGa" firstAttribute="leading" secondItem="V3o-XH-Xkn" secondAttribute="leading" id="sxO-BS-rki"/>                                 </constraints>                             </view>                             <navigationBar contentMode="scaleToFill" translucent="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SAV-Rj-fPh">                                 <rect key="frame" x="0.0" y="0.0" width="414" height="64"/>                                 <color key="barTintColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <textAttributes key="titleTextAttributes">                                     <fontDescription key="fontDescription" type="system" pointSize="18"/>                                     <color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>                                     <offsetWrapper key="textShadowOffset" horizontal="0.0" vertical="0.0"/>                                 </textAttributes>                             </navigationBar>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NmK-Xz-xl3" userLabel="Navigation Bar Hairline">                                 <rect key="frame" x="0.0" y="64" width="414" height="1"/>                                 <color key="backgroundColor" red="0.43079092750694548" green="0.11400473755687479" blue="0.11803794560598729" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                 <constraints>                                     <constraint firstAttribute="height" constant="1" id="ENM-UW-O3D" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                 </constraints>                             </view>                             <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="alk-Uo-pMv">                                 <rect key="frame" x="192" y="0.0" width="46" height="30"/>                                 <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>                                 <state key="normal" title="Button"/>                                 <connections>                                     <segue destination="G7K-pY-ZSi" kind="popoverPresentation" popoverAnchorView="alk-Uo-pMv" id="XuB-dB-CVh">                                         <popoverArrowDirection key="popoverArrowDirection" up="YES" down="YES" left="YES" right="YES"/>                                     </segue>                                 </connections>                             </button>                         </subviews>                         <constraints>                             <constraint firstItem="535-Cq-y2B" firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="trailing" id="0Ya-fi-fwL"/>                             <constraint firstItem="535-Cq-y2B" firstAttribute="trailing" secondItem="865-DH-bRZ" secondAttribute="trailing" id="2xc-7B-pd6"/>                             <constraint firstAttribute="trailing" secondItem="V3o-XH-Xkn" secondAttribute="trailing" id="4Qi-nQ-1b5"/>                             <constraint firstAttribute="trailing" secondItem="NmK-Xz-xl3" secondAttribute="trailing" id="4nX-tS-V49"/>                             <constraint firstAttribute="bottom" secondItem="V3o-XH-Xkn" secondAttribute="top" constant="54" id="8oE-54-0JN"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="top" secondItem="865-DH-bRZ" secondAttribute="top" id="DdA-dN-ucd"/>                             <constraint firstItem="V3o-XH-Xkn" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="KzS-R2-JSi"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="bottom" secondItem="gxR-oc-l6J" secondAttribute="bottom" constant="44" id="Okf-F1-Bwv">                                 <variation key="heightClass=regular-widthClass=regular" constant="64"/>                             </constraint>                             <constraint firstItem="NmK-Xz-xl3" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="Z7z-vD-Ypi"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="ZYY-Wz-1eh"/>                             <constraint firstAttribute="trailing" secondItem="SAV-Rj-fPh" secondAttribute="trailing" id="ein-Qv-Vwc"/>                             <constraint firstAttribute="bottom" secondItem="xLz-jb-9OM" secondAttribute="bottom" id="fmP-cS-nYy"/>                             <constraint firstItem="NmK-Xz-xl3" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="nYe-rf-Zht"/>                             <constraint firstItem="xLz-jb-9OM" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="tqI-Vx-dCH"/>                             <constraint firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="trailing" constant="-54" id="vuB-AY-U4j"/>                             <constraint firstItem="V3o-XH-Xkn" firstAttribute="top" secondItem="535-Cq-y2B" secondAttribute="bottom" id="wCH-Rs-saK"/>                             <constraint firstItem="535-Cq-y2B" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="yeH-A6-JEo"/>                         </constraints>                     </view>                     <extendedEdge key="edgesForExtendedLayout"/>                     <simulatedStatusBarMetrics key="simulatedStatusBarMetrics" statusBarStyle="lightContent"/>                     <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>                     <size key="freeformSize" width="414" height="736"/>                 </viewController>                 <placeholder placeholderIdentifier="IBFirstResponder" id="bpj-us-mEU" userLabel="First Responder" sceneMemberID="firstResponder"/>             </objects>             <point key="canvasLocation" x="681" y="-398"/>         </scene>         <!--View Controller-->         <scene sceneID="O4R-kZ-Pdk">             <objects>                 <viewController storyboardIdentifier="ContainerViewController" id="G7K-pY-ZSi" sceneMemberID="viewController">                     <layoutGuides>                         <viewControllerLayoutGuide type="top" id="ffW-Hu-TSB"/>                         <viewControllerLayoutGuide type="bottom" id="0mf-BW-Ssn"/>                     </layoutGuides>                     <view key="view" contentMode="scaleToFill" id="x2i-Md-44j">                         <rect key="frame" x="0.0" y="0.0" width="360" height="618"/>                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>                         <color key="backgroundColor" red="0.94235802664974622" green="0.91986702625601058" blue="0.90890064764795653" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                     </view>                 </viewController>                 <placeholder placeholderIdentifier="IBFirstResponder" id="B2f-Up-4gW" userLabel="First Responder" sceneMemberID="firstResponder"/>             </objects>             <point key="canvasLocation" x="1537.68115942029" y="-398.64130434782612"/>         </scene>     </scenes>     <inferredMetricsTieBreakers>         <segue reference="XuB-dB-CVh"/>     </inferredMetricsTieBreakers>     <color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/> </document> 

Copy everything inside a new project, remove main as the main storyboard to launch.

  1. Run the project on an iPhone X Plus simulator (or real device).
  2. Click on the bottom to present a view controller. 2.1. Enable slow motion (simulator only).
  3. Rotate your device.

The issue I'm talking about is when the yellow view is visible (window). One rotation does layout the presenting view controller correctly, the other does begin in wrong state.

enter image description here

Is that a bug that my storyboard has or something from UIKit?

Update: The project uses latest iOS SDK and no deprecated APIs (as shown above 'almost everything is build with IB').

Update 2: I updated the original sentence to make it crystal clear that it's not a UIPopoverController.

0 Answers

Read More

Sunday, February 5, 2017

iOS: Default IB popover transition does strange things

Leave a Comment

I bumped into a strange issue where when a popover view controller is presended, the presenting view controllers view is not snapped to the bottom edge of it's superview, in that case the window itself.

AppDelegate code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     // Override point for customization after application launch.     let window = UIWindow(frame: UIScreen.main.bounds)     window.rootViewController = UIStoryboard(name: "NavigationController", bundle: Bundle.main).instantiateInitialViewController()     window.makeKeyAndVisible()     window.backgroundColor = .yellow // Needed to detect the issue.     self.window = window      return true } 

Storyboard to test:

<?xml version="1.0" encoding="UTF-8"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="4lt-Df-IeL">     <device id="retina5_5" orientation="portrait">         <adaptation id="fullscreen"/>     </device>     <dependencies>         <deployment identifier="iOS"/>         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>     </dependencies>     <scenes>         <!--Navigation Controller (locked)-->         <scene sceneID="ryr-GW-wBz">             <objects>                 <viewController storyboardIdentifier="NavigationController" definesPresentationContext="YES" id="4lt-Df-IeL" userLabel="Navigation Controller (locked)" sceneMemberID="viewController">                     <layoutGuides>                         <viewControllerLayoutGuide type="top" id="gxR-oc-l6J"/>                         <viewControllerLayoutGuide type="bottom" id="Py7-tH-kHP"/>                     </layoutGuides>                     <view key="view" contentMode="scaleToFill" id="865-DH-bRZ" userLabel="Background View">                         <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>                         <subviews>                             <containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="535-Cq-y2B">                                 <rect key="frame" x="54" y="64" width="360" height="618"/>                                 <connections>                                     <segue destination="G7K-pY-ZSi" kind="embed" identifier="ContainerViewController" id="0Mw-mh-5NT"/>                                 </connections>                             </containerView>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="xLz-jb-9OM" userLabel="Left View">                                 <rect key="frame" x="0.0" y="64" width="54" height="672"/>                                 <subviews>                                     <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="bls-BC-WIu" userLabel="Left Stack View">                                         <rect key="frame" x="0.0" y="0.0" width="54" height="672"/>                                     </stackView>                                     <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Zbx-7u-5oe" userLabel="Hairline View">                                         <rect key="frame" x="53" y="0.0" width="1" height="672"/>                                         <color key="backgroundColor" red="0.43079092749999998" green="0.1140047376" blue="0.1180379456" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                         <constraints>                                             <constraint firstAttribute="width" constant="1" id="kna-vk-GlF" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                         </constraints>                                     </view>                                 </subviews>                                 <color key="backgroundColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <constraints>                                     <constraint firstAttribute="width" constant="54" id="2bA-Ap-aup"/>                                     <constraint firstItem="bls-BC-WIu" firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="leading" id="3sM-Bd-eEq"/>                                     <constraint firstAttribute="bottom" secondItem="Zbx-7u-5oe" secondAttribute="bottom" id="9PW-0y-kBv"/>                                     <constraint firstAttribute="trailing" secondItem="Zbx-7u-5oe" secondAttribute="trailing" id="RlN-G4-8iD"/>                                     <constraint firstAttribute="trailing" secondItem="bls-BC-WIu" secondAttribute="trailing" id="anE-HT-oQp"/>                                     <constraint firstItem="Zbx-7u-5oe" firstAttribute="top" secondItem="xLz-jb-9OM" secondAttribute="top" id="bxG-xS-wdV"/>                                     <constraint firstItem="bls-BC-WIu" firstAttribute="top" secondItem="xLz-jb-9OM" secondAttribute="top" id="ds3-z5-Dpv"/>                                     <constraint firstAttribute="bottom" secondItem="bls-BC-WIu" secondAttribute="bottom" id="f9r-FV-enN"/>                                 </constraints>                             </view>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="V3o-XH-Xkn" userLabel="Bottom View">                                 <rect key="frame" x="0.0" y="682" width="414" height="54"/>                                 <subviews>                                     <stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="fM3-hA-kGa" userLabel="Bottom Stack View">                                         <rect key="frame" x="0.0" y="0.0" width="414" height="54"/>                                     </stackView>                                     <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Gtr-bO-frP" userLabel="Hairline View">                                         <rect key="frame" x="0.0" y="0.0" width="414" height="1"/>                                         <color key="backgroundColor" red="0.43079092749999998" green="0.1140047376" blue="0.1180379456" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                         <constraints>                                             <constraint firstAttribute="height" constant="1" id="K5f-m2-BpE" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                         </constraints>                                     </view>                                 </subviews>                                 <color key="backgroundColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <constraints>                                     <constraint firstItem="Gtr-bO-frP" firstAttribute="leading" secondItem="V3o-XH-Xkn" secondAttribute="leading" id="9fX-NI-RdG"/>                                     <constraint firstAttribute="bottom" secondItem="fM3-hA-kGa" secondAttribute="bottom" id="KxS-jW-8Rh"/>                                     <constraint firstItem="fM3-hA-kGa" firstAttribute="top" secondItem="V3o-XH-Xkn" secondAttribute="top" id="VLk-U3-Dgt"/>                                     <constraint firstAttribute="trailing" secondItem="Gtr-bO-frP" secondAttribute="trailing" id="aDz-aj-8ah"/>                                     <constraint firstAttribute="trailing" secondItem="fM3-hA-kGa" secondAttribute="trailing" id="bzw-Bv-nvH"/>                                     <constraint firstAttribute="height" constant="54" id="ghd-qS-t3l"/>                                     <constraint firstItem="Gtr-bO-frP" firstAttribute="top" secondItem="V3o-XH-Xkn" secondAttribute="top" id="nZz-Tn-i7X"/>                                     <constraint firstItem="fM3-hA-kGa" firstAttribute="leading" secondItem="V3o-XH-Xkn" secondAttribute="leading" id="sxO-BS-rki"/>                                 </constraints>                             </view>                             <navigationBar contentMode="scaleToFill" translucent="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SAV-Rj-fPh">                                 <rect key="frame" x="0.0" y="0.0" width="414" height="64"/>                                 <color key="barTintColor" red="0.627" green="0.114" blue="0.14099999999999999" alpha="1" colorSpace="calibratedRGB"/>                                 <textAttributes key="titleTextAttributes">                                     <fontDescription key="fontDescription" type="system" pointSize="18"/>                                     <color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>                                     <offsetWrapper key="textShadowOffset" horizontal="0.0" vertical="0.0"/>                                 </textAttributes>                             </navigationBar>                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NmK-Xz-xl3" userLabel="Navigation Bar Hairline">                                 <rect key="frame" x="0.0" y="64" width="414" height="1"/>                                 <color key="backgroundColor" red="0.43079092750694548" green="0.11400473755687479" blue="0.11803794560598729" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                                 <constraints>                                     <constraint firstAttribute="height" constant="1" id="ENM-UW-O3D" customClass="OnePixelConstraint" customModule="segue_issue" customModuleProvider="target"/>                                 </constraints>                             </view>                             <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="alk-Uo-pMv">                                 <rect key="frame" x="192" y="0.0" width="46" height="30"/>                                 <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>                                 <state key="normal" title="Button"/>                                 <connections>                                     <segue destination="G7K-pY-ZSi" kind="popoverPresentation" popoverAnchorView="alk-Uo-pMv" id="XuB-dB-CVh">                                         <popoverArrowDirection key="popoverArrowDirection" up="YES" down="YES" left="YES" right="YES"/>                                     </segue>                                 </connections>                             </button>                         </subviews>                         <constraints>                             <constraint firstItem="535-Cq-y2B" firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="trailing" id="0Ya-fi-fwL"/>                             <constraint firstItem="535-Cq-y2B" firstAttribute="trailing" secondItem="865-DH-bRZ" secondAttribute="trailing" id="2xc-7B-pd6"/>                             <constraint firstAttribute="trailing" secondItem="V3o-XH-Xkn" secondAttribute="trailing" id="4Qi-nQ-1b5"/>                             <constraint firstAttribute="trailing" secondItem="NmK-Xz-xl3" secondAttribute="trailing" id="4nX-tS-V49"/>                             <constraint firstAttribute="bottom" secondItem="V3o-XH-Xkn" secondAttribute="top" constant="54" id="8oE-54-0JN"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="top" secondItem="865-DH-bRZ" secondAttribute="top" id="DdA-dN-ucd"/>                             <constraint firstItem="V3o-XH-Xkn" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="KzS-R2-JSi"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="bottom" secondItem="gxR-oc-l6J" secondAttribute="bottom" constant="44" id="Okf-F1-Bwv">                                 <variation key="heightClass=regular-widthClass=regular" constant="64"/>                             </constraint>                             <constraint firstItem="NmK-Xz-xl3" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="Z7z-vD-Ypi"/>                             <constraint firstItem="SAV-Rj-fPh" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="ZYY-Wz-1eh"/>                             <constraint firstAttribute="trailing" secondItem="SAV-Rj-fPh" secondAttribute="trailing" id="ein-Qv-Vwc"/>                             <constraint firstAttribute="bottom" secondItem="xLz-jb-9OM" secondAttribute="bottom" id="fmP-cS-nYy"/>                             <constraint firstItem="NmK-Xz-xl3" firstAttribute="leading" secondItem="865-DH-bRZ" secondAttribute="leading" id="nYe-rf-Zht"/>                             <constraint firstItem="xLz-jb-9OM" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="tqI-Vx-dCH"/>                             <constraint firstAttribute="leading" secondItem="xLz-jb-9OM" secondAttribute="trailing" constant="-54" id="vuB-AY-U4j"/>                             <constraint firstItem="V3o-XH-Xkn" firstAttribute="top" secondItem="535-Cq-y2B" secondAttribute="bottom" id="wCH-Rs-saK"/>                             <constraint firstItem="535-Cq-y2B" firstAttribute="top" secondItem="SAV-Rj-fPh" secondAttribute="bottom" id="yeH-A6-JEo"/>                         </constraints>                     </view>                     <extendedEdge key="edgesForExtendedLayout"/>                     <simulatedStatusBarMetrics key="simulatedStatusBarMetrics" statusBarStyle="lightContent"/>                     <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>                     <size key="freeformSize" width="414" height="736"/>                 </viewController>                 <placeholder placeholderIdentifier="IBFirstResponder" id="bpj-us-mEU" userLabel="First Responder" sceneMemberID="firstResponder"/>             </objects>             <point key="canvasLocation" x="681" y="-398"/>         </scene>         <!--View Controller-->         <scene sceneID="O4R-kZ-Pdk">             <objects>                 <viewController storyboardIdentifier="ContainerViewController" id="G7K-pY-ZSi" sceneMemberID="viewController">                     <layoutGuides>                         <viewControllerLayoutGuide type="top" id="ffW-Hu-TSB"/>                         <viewControllerLayoutGuide type="bottom" id="0mf-BW-Ssn"/>                     </layoutGuides>                     <view key="view" contentMode="scaleToFill" id="x2i-Md-44j">                         <rect key="frame" x="0.0" y="0.0" width="360" height="618"/>                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>                         <color key="backgroundColor" red="0.94235802664974622" green="0.91986702625601058" blue="0.90890064764795653" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>                     </view>                 </viewController>                 <placeholder placeholderIdentifier="IBFirstResponder" id="B2f-Up-4gW" userLabel="First Responder" sceneMemberID="firstResponder"/>             </objects>             <point key="canvasLocation" x="1537.68115942029" y="-398.64130434782612"/>         </scene>     </scenes>     <inferredMetricsTieBreakers>         <segue reference="XuB-dB-CVh"/>     </inferredMetricsTieBreakers>     <color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/> </document> 

Copy everything inside a new project, remove main as the main storyboard to launch.

  1. Run the project on an iPhone X Plus simulator (or real device).
  2. Click on the bottom to present a view controller. 2.1. Enable slow motion (simulator only).
  3. Rotate your device.

The issue I'm talking about is when the yellow view is visible (window). One rotation does layout the presenting view controller correctly, the other does begin in wrong state.

enter image description here

Is that a bug that my storyboard has or something from UIKit?

0 Answers

Read More

Sunday, March 27, 2016

Different size classes for iPad portal and landscape modes with containerviews

Leave a Comment

I've found this question and I've tried to implement the solution that has been given. However I run into a problem.

My initial view controller has two container views who both have their own view controller. I've created a root view controller that is assigned to the initial view controller. The code in this class looks like this.

class RootViewController: UIViewController {  var willTransitionToPortrait: Bool! var traitCollection_CompactRegular: UITraitCollection! var traitCollection_AnyAny: UITraitCollection!  override func viewDidLoad() {     super.viewDidLoad()     setupReferenceSizeClasses()     // Do any additional setup after loading the view. }  override func viewWillAppear(animated: Bool) {     willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width }  override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {     willTransitionToPortrait = size.height > size.width }  func setupReferenceSizeClasses(){     let traitCollection_hCompact = UITraitCollection(horizontalSizeClass: .Compact)     let traitCollection_vRegular = UITraitCollection(verticalSizeClass: .Regular)     traitCollection_CompactRegular = UITraitCollection(traitsFromCollections: [traitCollection_hCompact, traitCollection_vRegular])      let traitCollection_hAny = UITraitCollection(horizontalSizeClass: .Unspecified)     let traitCollection_vAny = UITraitCollection(verticalSizeClass: .Unspecified)     traitCollection_AnyAny = UITraitCollection(traitsFromCollections: [traitCollection_hAny, traitCollection_vAny]) }  override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {     let traitCollectionForOverride = ((willTransitionToPortrait) != nil) ? traitCollection_CompactRegular : traitCollection_AnyAny      return traitCollectionForOverride; } 

However when I run it the size class won't respons like it should. One of the container view controllers will start acting weird in both landscape and portrait mode like can be seen below.

landscape mode portrait mode

When I don't assign the rootviewcontroller it will look like this landscape mode portrait mode

While it should look like this in portrait mode portrait mode how it should look

Does anyone know what might be going wrong here? Why it doesn't change the size class like desired.

EDIT Like @Muhammad Yawar Ali asked here are screenshots from the position of all the size classes I've set. I have no warnings or errors on any constraints so these screenshots contain the updated views.

enter image description here enter image description here

I hope this shows everything that is needed.

EDIT: for some reason I'm unable to put in all the screenshots

2 Answers

Answers 1

On the viewWillTransitionToSize you need to call also super, to pass the event to the next responder (your rootviewcontroller)...

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {     super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)     willTransitionToPortrait = size.height > size.width } 

Answers 2

I have cloned your code from repository : https://github.com/MaikoHermans/sizeClasses.git And editted code put the below code in you controller it will work fine & will not effect your design in iPads. import UIKit

class RootViewController: UIViewController {     override func viewDidLoad() {       super.viewDidLoad()       // Do any additional setup after loading the view.    }     override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {       if view.bounds.width < view.bounds.height {          return UITraitCollection(horizontalSizeClass: .Unspecified)       } else {          return UITraitCollection(horizontalSizeClass: .Regular)       }    } } 

You can try with this code but There is an issue i believe its not updating traits properly for ipads and view layout remains same but looks good. I have tried multiple ways but not succeeded yet will update my answer.

Read More