Auto Layout

Create a user interface that dynamically responds to changes in the available screen space using Auto Layout.

Posts under Auto Layout tag

77 results found
Post marked as solved
162 Views

UIButton embedded in view which is then embedded in UIstackview not working

I have a UIButton of which I have a touchUpInside IBAoutlet function call, and it is embedded inside a view, which is then embedded inside a stackview. I have User Interaction enabled for all three of of the stackview, the view and the button. However, the touchUpInside function is not called when I click the button. Does anyone know if this has a solution?
Asked
Last updated .
Post marked as unsolved
129 Views

Do you have to set translatesAutoresizingMaskIntoConstraints to false for a ViewController's 'main' view?

I have a blank Swift project in Xcode and am not using storyboard. I have a view that I've added in code under viewDidLoad: var view1 = UIView() view1.backgroundColor = .red view.addSubview(view1) view1.translatesAutoresizingMaskIntoConstraints = false I then add constraints: NSLayoutConstraint.activate([ view1.topAnchor.constraint(equalTo: view.topAnchor, constant: 20), view1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20), view1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20), view1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20) ]) The view loads and looks fine in the simulator. But I've been having some issues with some other views in another project so I have built the above for testing. So what I've discovered is that even though the above builds and works, if I check for ambiguous layouts using: print(view1.hasAmbiguousLayout) I get errors logged: 2021-02-14 15:26:34.919048+1100 autolayoutextensions[6603:4438601] [LayoutConstraints] View has an ambiguous layout. See "Auto Layout Guide: Ambiguous Layouts" for help debugging. Displaying synopsis from invoking -[UIView _autolayoutTrace] to provide additional detail.UIView:0x13360c3b0- AMBIGUOUS LAYOUT for UIView:0x13360c3b0.Width{id: 9}, UIView:0x13360c3b0.Height{id: 12} Legend: * - is laid out with auto layout + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES • - layout engine host true The only way I can get these errors to NOT get logged, is by also setting the ViewController's main view as follows: view.translatesAutoresizingMaskIntoConstraints = false By doing this, I know get the following log output: false So my question is this... I haven't read anywhere in Apple's documentation (or here, Googling etc) that says you have to set the viewController's view.translatesAutoresizingMaskIntoConstraints to false. The documentation only talks about views that are added . Have I missed something? Apologies if this is just my understanding but would be good to get this clarified.
Asked
by anthonycj.
Last updated .
Post marked as unsolved
168 Views

Button title and accessibility for fonts

Currently working on a medical at home testing app that has to comply with certain FDA guidelines for accessibility. Mainly, larger font sizes. I understand how Dynamic Type works and that seems to be working fine so far - however, when I add a UIButton to my nib and set its Title font to style "Title 3" (for example, so that it scales with System settings), the font scales up and becomes invisible because the button itself is not scaling. I end up with an ellipse for the button Title. Also, if the button did scale it would really impact the UI in terms of the design that I am coding to. Is there any best practice or guide for how to handle this? Should I just ignore button accessibility? Any tips or advice is appreciated.
Asked
Last updated .
Post marked as unsolved
243 Views

UICollectionViewCompositionalLayout orthogonalScrollingBehavior scroll view is incorrectly positioned when using estimated item heights

I have created Feedback for a regression found in iOS 14.3. Our Apple rep suggested that I post here as well in case any UIKit engineers are looking out for issues on the dev forums. The feedback is FB8965336 (REGRESSION: UICollectionViewCompositionalLayout orthogonalScrollingBehavior scroll view is incorrectly positioned when using estimated item heights). The issue is caused by the use of dynamically sized cells which can have different heights based on their content. Something changed in 14.3 which causes the dynamic cell heights to be handled incorrectly when using compositional layouts in UICollectionView. This manifests itself by moving the area on the screen that you swipe to scroll an orthogonal section to be either above or below the section. Here is a SO post (not created by me) which describes the issue: https://stackoverflow.com/questions/65369130/uicollectionviewcompositionallayout-bug-on-ios-14-3 I have posted a sample project in the Feedback along with a video with a side by side comparison of the behavior on 14.2 and 14.3. Thank you.
Asked
by craignyt.
Last updated .
Post marked as unsolved
133 Views

[Base Internationalization + Auto Layout] How to do the following tasks?

Auto Layout: How do you get the width of a column in a NSTableView to resize correctly to fit the localized header column title? Base Internationalization: Is it possible to hide a view for a specific language using the .strings files? For instance, is there something like: "id-view-something.hidden" = YES; ? If you add a NSTextField to a xib file, do you really need to manually add the NSTextFieldCell id in every existing corresponding .strings file? Or is there a command in Xcode to update the .strings files (Xcode 10.3 here)? I've already had a look at the documentation, stackoverflow and so on. The only answer found was for the 3rd question and it looked like a 3rd party tool is required to achieve this task.
Asked
Last updated .
Post marked as unsolved
114 Views

Need tips on adding AutoLayout UIView programmatically with Safe Area

I have a UIView that i create with a XIB. I then want to add it on top of a parent, take up the whole screen but use the safe area i have set up constraints for in the xib. I can add the view fine, but when i do, the safe area is ignored, and the view goes all the way to the top and bottom. The code i use (objective-c still sorry): (UIView *parent) MyViewController *newViewController = [[MyViewController alloc] initWithNibName:MY_VIEW_NIB_NAME bundle:nil]; [view setFrame:CGRectMake(0, 0, parent.bounds.size.width, parent.bounds.size.height)]; [parent addSubview:self.view]; In the Xib, i have the top and bottom constrained to SafeArea.top and bottom. It looks fine in the layout there. Any tips are appreciated.
Asked
by Mathias_.
Last updated .
Post marked as unsolved
94 Views

translatesAutoresizingMaskIntoConstraints

When self.window is the navigationcontroller and the translatesAutoresizingMaskIntoConstraints of the root view = false, I set the background color (self.view.backgroundColor = UIColor.orange) but it does not take effect. When translatesAutoresizingMaskIntoConstraints = true, everything becomes normal again. One more thing, when self.window is UIViewController, translatesAutoresizingMaskIntoConstraints = false everything will become normal. Why navigationcontroller and UIViewController are so different. who can help me! Thank you!
Asked
by NMXY.
Last updated .
Post marked as unsolved
81 Views

Portrait only on iPhone but any orientation on iPad?

I'd like my app to run in Portrait mode only on iPhones, but any orientation on iPad. Years ago, I recall being able to do this by overriding or implementing some callback functions for orientation, which seemed to change with every other iOS release. I'm hoping there is a simpler way to do this today. I was hoping that the Deployment Info tab in Xcode would let me set different Device Orientation choices for iPhone and iPad, but this doesn't seem to be the case. I'm sure I could hack around and come up with some solution, but I'd like to learn what the "right" way is to do this in current versions of iOS. Thanks, Frank
Asked
by flarosa.
Last updated .
Post marked as unsolved
83 Views

Launch Screen Storyboard with Tab Bar Alone and in Split View

My 2015 universal app needing an update apparently needs a Launch Screen storyboard. The iPhone app (for smaller phones in portrait and landscape) is basically a tab bar controller connected to several navigable table view controllers displaying first a topics table page and then details. For larger iPhones and iPads, this is all done programmatically in my code and becomes a split view with a column for topics and a larger area for details. The current app uses dozens of screen shots for each device and orientation. With Xcode 12, I'm at a loss of how to create a single launch screen that will display the tab bar controller and an empty table (which was easy), yet magically move the tab bar controller to the left and the details on the larger area on the right for a split view. Is there any documentation/videos/tutorials of how to manage this? If it was just iPhones and iPads I could make two launch storyboards but I don't know how to include large iPhones.
Asked
by DrMiller.
Last updated .
Post marked as unsolved
80 Views

Converting old project to use Autolayout

I'm having a very difficult time converting an old project to Autolayout. I already removed my old launch images and replaced them with a Launch Screen, which causes the app to at least not run in the letterboxed mode anymore. However, when I open each view (XIB file) Interface Builder is mostly showing me a bunch of blue boxes with no detail in them where my UI elements should be. I made sure that the Opens In and Builds For settings are correct, and also checked Use Trait Variations and Use Safe Area Layout Guides. I also tried creating a new view from scratch, but I ran into another problem: Interface Builder won't let me drag new UI elements into the main View as subviews. It only lets me drop them as new top-level views. I can copy and paste the view where I want it, but Autolayout appears to be disabled: I can't make any constraints. It seems like there must be some global setting that is preventing the app from behaving like a modern app, but I can't figure out what it is. Thanks, Frank
Asked
by flarosa.
Last updated .
Post marked as unsolved
96 Views

iPhone Landscape right or left doesn't work

Hi everybody. I know that in iPhone X or iPhones that has the notch the upside down orientation doesn't work but when I rotate in landscape right or left passing by upside down orientation, iPhone doesn't detect the orientation. how can I detect these changes of orientation?
Asked
by Rufy.
Last updated .
Post marked as unsolved
84 Views

What can the intrinsic content size provide that the Autolayout anchors can't

I'm having difficult time understanding the practical usage of the intrinsic content size, especially the cases when you have to override the existing intrinsic size. It seems like using the widthAnchor and the heightAnchor seem to resolve what the intrinsic content size is trying to solve. For example if I wanted a certain intrinsic size: class CustomView: UIView { 		override var intrinsicContentSize: CGSize { 				return CGSize(width: 200, height: 100) 		} } I could achieve the same effect by using the Autolayout  anchors: NSLayoutConstraint.activate([ 		view.heightAnchor.constraint(equalToConstant: 100), 		view.widthAnchor.constraint(equalToConstant: 200) ]) When the stack view are required to have a size, you can also provide it with the anchors. Only two use cases I found it to be helpful is if you wanted to manipulate the existing original intrinsicContentSize: class CustomView: UIView { 		override var intrinsicContentSize: CGSize { 				let originalSize = super.intrinsicContentSize 				return CGSize(width: originalSize.width + 200, height: 100) 		} } But, I feel like intrinsicContentSize offers far more than simply using it as margins. The second use case is when you can change the priority by using either setContentCompressionResistancePriority(_:for:) or setContentHuggingPriority(_:for:) to indicate how to respond to the changes in its superview dynamically.
Asked
by Ovis.
Last updated .
Post marked as solved
623 Views

How to use additionalSafeAreaInsets

I'm trying to implement additionalSafeAreaInsets, similar to the example from the Apple's documentation, - https://developer.apple.com/documentation/uikit/uiview/positioning_content_relative_to_the_safe_area but the child view controller is not respecting the additional insets: import UIKit import PlaygroundSupport class MyVC: UIViewController { 		var leftView: UIView! 		let secondVC = SecondVC() 		 		override func viewDidLoad() { 				super.viewDidLoad() 				leftView = UIView(frame: CGRect(origin: .zero, size: .init(width: 50, height: self.view.bounds.size.height))) 				leftView.backgroundColor = .blue 				self.view.addSubview(leftView) 		} 		 		override func viewDidAppear(_ animated: Bool) { 				super.viewDidAppear(true) 						 				var newSafeArea = UIEdgeInsets() 				if let sideViewWidth = leftView?.bounds.size.width { 						newSafeArea.left += sideViewWidth 				} 				 				print(newSafeArea) 				 				addChild(secondVC) 				secondVC.view.frame = self.view.safeAreaLayoutGuide.layoutFrame 				self.view.addSubview(secondVC.view) 				secondVC.didMove(toParent: self) 				secondVC.additionalSafeAreaInsets = newSafeArea 		} } class SecondVC: UIViewController { 		override func viewDidLoad() { 				super.viewDidLoad() 				self.view.backgroundColor = .yellow 		} } PlaygroundPage.current.needsIndefiniteExecution = true PlaygroundPage.current.liveView = MyVC() The expectation is that the left column view (leftView) is juxtaposed to a child view controller (childVC) within a root view controller (MyVC), but the child view controller simply takes up the entire main view.
Asked
by Ovis.
Last updated .
Post marked as unsolved
113 Views

How do you use UIEdgeInsets?

I've been trying different ways on the Playground, but the margins are not being implemented. I'm trying to understand the proper way of using UIEdgeInsets (and NSDirectionalEdgeInsets). My expectation is that the super view should have margins of specified size and the subview should be within that margin, sort of like an invisible border. Following didn't show any margins for the superview: import UIKit import PlaygroundSupport let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) rootView.layoutMargins = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100) let subview = UIView() subview.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([   subview.widthAnchor.constraint(equalToConstant: 100),   subview.heightAnchor.constraint(equalToConstant: 100) ]) subview.backgroundColor = .red rootView.addSubview(subview) PlaygroundPage.current.needsIndefiniteExecution = true PlaygroundPage.current.liveView = rootView I've also tried with: rootView.layoutMargins.top = 100 or instead of Autolayout: let subview = UIView(frame: CGRect(origin: .zero, size: .init(width: 100, height: 100))) No need to mention NSDirectionEdgeInsets didn't work: rootView.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 100, leading: 100, bottom: 100, trailing: 100) I tried it with a view controller, but still futile: class MyVC: UIViewController {   override func viewDidLoad() {     super.viewDidLoad()           let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))     rootView.layoutMargins = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)     self.view.addSubview(rootView)           let subview = UIView()     subview.translatesAutoresizingMaskIntoConstraints = false     NSLayoutConstraint.activate([       subview.widthAnchor.constraint(equalToConstant: 100),       subview.heightAnchor.constraint(equalToConstant: 100)     ])     subview.backgroundColor = .red     rootView.addSubview(subview)   } } What am I doing wrong?
Asked
by Ovis.
Last updated .
Post marked as unsolved
109 Views

Layout in Storyboard is different from Simulator

I really need help here. I just started programming a game in Xcode and I already have a problem with the layout on a iPhone 11 Pro Max. In the Storyboard the layout looks the way I want it to, but if I run the simulations the layout always looks like the one for iPhone 4. I have black bars over and under the game. How can I fix this ?
Asked
by Philipp_G.
Last updated .