Auto Layout

RSS for tag

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

Posts under Auto Layout tag

48 Posts
Sort by:
Post not yet marked as solved
12 Replies
3.3k Views
I have iPhone 11 Pro running iOS 14 beta 2. In one of my projects (and only that project perhaps), the view controller doesn't autorotate when connected to XCode (11 or 12) debugger. When not connected to debugger, there is no issue in autorotation. This happens for only one particular project and it's not clear what could be blocking autorotation. None of the autorotation methods are called. I wonder what could be blocking autorotation. And if I connect any iOS 13 device, autorotation works with the debugger. Is this a known issue with iOS 14/XCode?
Posted
by
Post marked as solved
1 Replies
679 Views
I am trying to create a UIView within a UIStackView that I want to always be square regardless of device and orientation. I have seen posting that explain how others have done it (setting constraints between UIView and superview for both height and width - a required set and an optional set - along with a 1:1 aspect constraint for the UIView) , but it no longer seems to work under all instances - in particular iPad and iPhone 11s. Does anyone have any advice on how to make this happen. It seems to me to be a pretty routine thing to do. I've done it before programmatically, but I would really like to get it done within Interface Builder. Any suggestions would be greatly appreciated.
Posted
by
Post not yet marked as solved
5 Replies
2.0k Views
Using NSCollectionLayoutSize with .estimated dimensions in horizontal orthogonal sections, creates layout issues. The cells & supplementary views have layout conflicts, the scroll behavior is sub optimal and spacing is not as expected Working with Xcode: 12.4 , Simulator: iOS 14.4 Layout bug: [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "NSLayoutConstraint:0x6000011266c0 UIView:0x7fc6c4617020.height == 80 (active)", "NSLayoutConstraint:0x600001126530 V:|-(0)-[UIView:0x7fc6c4617020] (active, names: '|':UIView:0x7fc6c4616d10 )", "NSLayoutConstraint:0x6000011261c0 UIView:0x7fc6c4617020.bottom == UIView:0x7fc6c4616d10.bottom (active)", "NSLayoutConstraint:0x600001121360 'UIView-Encapsulated-Layout-Height' UIView:0x7fc6c4616d10.height == 50 (active)" ) Will attempt to recover by breaking constraint NSLayoutConstraint:0x6000011266c0 UIView:0x7fc6c4617020.height == 80 (active) Code to reproduce: import UIKit class ViewController: UIViewController { lazy var collectionView: UICollectionView = { let layout = createLayout() let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.dataSource = self collectionView.backgroundColor = .systemBackground collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell") collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header") return collectionView }() private func createLayout() - UICollectionViewCompositionalLayout { let sectionProvider = { (section: Int, layoutEnvironment: NSCollectionLayoutEnvironment) - NSCollectionLayoutSection? in return self.horizontalLayout(layoutEnvironment: layoutEnvironment) } let config = UICollectionViewCompositionalLayoutConfiguration() config.interSectionSpacing = 8 let layout = UICollectionViewCompositionalLayout(sectionProvider: sectionProvider, configuration: config) return layout } private func supplementaryHeader() - NSCollectionLayoutBoundarySupplementaryItem { let titleSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(50)) let titleSupplementary = NSCollectionLayoutBoundarySupplementaryItem( layoutSize: titleSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top) return titleSupplementary } private func horizontalLayout(layoutEnvironment: NSCollectionLayoutEnvironment) - NSCollectionLayoutSection { let size = NSCollectionLayoutSize(widthDimension: .estimated(120), heightDimension: .estimated(50)) let item = NSCollectionLayoutItem(layoutSize: size) let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item]) let section = NSCollectionLayoutSection(group: group) section.orthogonalScrollingBehavior = .continuous section.interGroupSpacing = 8 section.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16) section.boundarySupplementaryItems = [supplementaryHeader()] return section } override func viewDidLoad() { super.viewDidLoad() view.addSubview(collectionView) NSLayoutConstraint.activate([ collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), collectionView.topAnchor.constraint(equalTo: view.topAnchor), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) } } // MARK: UICollectionViewDataSource extension ViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) return cell } func numberOfSections(in collectionView: UICollectionView) - Int { return 25 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int { return 4 } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) - UICollectionReusableView { switch kind { case UICollectionView.elementKindSectionHeader: let header: HeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! HeaderView return header default: fatalError() } } } class Cell: UICollectionViewCell { lazy var view: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .systemRed return view }() override init(frame: CGRect) { super.init(frame: frame) configure() } required init?(coder: NSCoder) { fatalError("not implemented") } func configure() { contentView.addSubview(view) view.heightAnchor.constraint(equalToConstant: 80).isActive = true view.widthAnchor.constraint(equalToConstant: 100).isActive = true NSLayoutConstraint.activate([ view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), view.topAnchor.constraint(equalTo: contentView.topAnchor), view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) } } class HeaderView: UICollectionReusableView { lazy var view: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .systemTeal return view }() override init(frame: CGRect) { super.init(frame: frame) configure() } required init?(coder: NSCoder) { fatalError("not implemented") } func configure() { addSubview(view) view.heightAnchor.constraint(equalToConstant: 60).isActive = true NSLayoutConstraint.activate([ view.leadingAnchor.constraint(equalTo: self.leadingAnchor), view.trailingAnchor.constraint(equalTo: self.trailingAnchor), view.topAnchor.constraint(equalTo: self.topAnchor), view.bottomAnchor.constraint(equalTo: self.bottomAnchor) ]) } }
Posted
by
Post not yet marked as solved
8 Replies
2.7k Views
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Not possible to remove variable: 400: unknown var (bug!) with engine as delegate:0x283c8ed40{id: 33112} colIndex:32 from engine NSISEngine: 0x129248130{ delegate:0x12934eaa0 EngineVars:  0: objective{id: 33102} rowIndex:0  1: UIImageView:0x12924ec90.Height{id: 32592} rowIndex:1073741824  2: 0x280a04be0.marker{id: 32591} colIndex:1073741825  3: UIImageView:0x12924ec90.Width{id: 32807} rowIndex:1  4: 0x280dc35a0.posErrorMarker{id: 32871} colIndex:1  5: 0x280dc35a0.negError{id: 32872} rowIndex:88  6: 0x280dc3d80.posErrorMarker{id: 32873} colIndex:1073741826  7: 0x280dc3d80.negError{id: 32874} rowIndex:1073741825  8: UIImageView:0x129353f00.Width{id: 32594} rowIndex:2  9: 0x280979d60.marker{id: 32593} colIndex:3 10: 0x280dc1e60.posErrorMarker{id: 32875} colIndex:4 11: 0x280dc1e60.negError{id: 32876} rowIndex:3 12: UIImageView:0x129353f00.Height{id: 32822} rowIndex:1073741826 13: 0x280dc39c0.posErrorMarker{id: 32877} colIndex:1073741827 14: 0x280dc39c0.negError{id: 32878} rowIndex:1073741898 15: UIButton:0x1293540d0.Width{id: 32817} rowIndex:4 16: 0x280dc2040.posErrorMarker{id: 32879} rowIndex:93 17: 0x280dc2040.negError{id: 32880} colIndex:6 18: UIButton:0x1293540d0.Height{id: 32820} rowIndex:1073741827 19: 0x280dc3de0.posErrorMarker{id: 32881} rowIndex:96 20: 0x280dc3de0.negError{id: 32882} colIndex:1073741830 21: UILabel:0x129353300.Width{id: 32811} rowIndex:5 22: 0x280dc3e40.posErrorMarker{id: 32883} colIndex:44 23: 0x280dc3e40.negError{id: 32884} rowIndex:90 24: UILabel:0x129353300.Height{id: 32842} colIndex:1073741828 25: 0x280dc27c0.posErrorMarker{id: 32885} colIndex:1073741831 26: 0x280dc27c0.negError{id: 32886} rowIndex:1073741828 27: UILabel:0x129353c90.Width{id: 32840} rowIndex:6 28: 0x280dc1da0.posErrorMarker{id: 32887} colIndex:9 29: 0x280dc1da0.negError{id: 32888} rowIndex:97 30: UILabel:0x129353c90.Height{id: 32829} colIndex:1073741832 31: 0x280dc31e0.posErrorMarker{id: 32889} colIndex:1073741833 32: 0x280dc31e0.negError{id: 32890} rowIndex:1073741900 33: UIImageView:0x12934e490.Width{id: 32596} rowIndex:1073741905 34: UIImageView:0x12934e490.Height{id: 32597} colIndex:1073741844 35: 0x280963a70.marker{id: 32595} colIndex:11 36: 0x280dc2ee0.posErrorMarker{id: 32891} colIndex:12 37: 0x280dc2ee0.negError{id: 32892} rowIndex:8 38: 0x280dc1d40.posErrorMarker{id: 32893} colIndex:1073741835 39: 0x280dc1d40.negError{id: 32894} rowIndex:1073741830 40: UIButton:0x1293543b0.Width{id: 32832} rowIndex:9 41: 0x280dc1440.posErrorMarker{id: 32895} colIndex:0 42: 0x280dc1440.negError{id: 32896} rowIndex:1073741845 43: UIButton:0x1293543b0.Height{id: 32827} rowIndex:1073741831 44: 0x280dc3840.posErrorMarker{id: 32897} colIndex:1073741836 45: 0x280dc3840.negError{id: 32898} rowIndex:1073741829 46: UILabel:0x129353a20.Width{id: 32848} rowIndex:10 and another thousand or so lines. this crash occurs after I change the language and open the workout page to start a workout immediately, and sometimes on the second time, I open the workout page.
Posted
by
Post not yet marked as solved
0 Replies
556 Views
I have an NSTextView inside an NSScrollView with magnification. Zooming in with a pinch-and-zoom gesture works fine, but if I then resize the window while zoomed in, it will eventually crash with the error The window has been marked as needing another Display Window pass, but it has already had more Display Window passes than there are views in the window. It does not always happen immediately, sometimes you have to keep resizing for a while to trigger it. I have made a video illustrating the problem here: youtu.be/M_JRQI2oDaY The original code is here: https://github.com/angstsmurf/spatterlight/tree/helpviewtest (The branch helpviewtest is a cut down test case created for this problem.)
Posted
by
Post not yet marked as solved
1 Replies
347 Views
I want to build Instagram like comment - replies UI. Here are few questions. 1 - Nested CollectionView. I first built comments UI with collectionView. But now facing self-cell sizing issue on how to update the height of cell dynamically based on the nested collectionView of replies. 2 - Nested Tableview. So I thought maybe with automatic dimension set up with tableview. It'd be much simpler to build comment-replies UI. But I read on Stack Overflow that nested tableViews are not recommended because it might mess up with scrolls . 3 - So what would be the best way to build comment-replies UI just like Instagram?
Posted
by
Post marked as solved
1 Replies
792 Views
I am using a UIView with a nib as a template for a UIImage that I am generating, and I want to handle the output of the iPad in landscape differently than portrait. The best way I have figured I can do that is by setting up my landscape view for horizontal Regular, vertical Compact traits in the nib and assigning those traits before generating the image. I have tried using the performAsCurrent method which successfully changes the UITraitCollection.current value but does not affect my UIView traitCollection property, and I have tried overriding the traitCollection getter in the UIView class which returns the error: Class overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API. Is there a way to do this for a UIView that is never drawn to the screen?
Posted
by
Post not yet marked as solved
2 Replies
832 Views
Hello, Please help me solve this error: IB Designables: Failed to render and update auto layout status for ViewController (BYZ-38-t0r): dlopen(Demo.app, 1): no suitable image found. Did find: Demo.app: mach-o, but not built for platform iOS-sim I create new simple application. Then I create custom view with IB Designables and this broke Storyboard. I have a lot of projects in which I use Storyboard and now I cannot edit them. Thanks. Xcode: 12.5.1 (12E507) MacOS: 11.5.1 (20G80) Chip: Apple M1
Posted
by
Post not yet marked as solved
0 Replies
328 Views
What might be a good way to constrain a view's top anchor to be just at the edge of a device's Face ID sensor housing if it has one? This view is a product photo that would be clipped too much if it ignored the top safe area inset, but if it was positioned relative to the top safe area margin this wouldn't be ideal either because of the slight gap between the sensor housing and the view (the view is a photo of pants cropped at the waist). What might be a good approach here?
Posted
by
Post not yet marked as solved
1 Replies
863 Views
Hello! I am having trouble with my quiz game - sometimes (not always) after completing a level, the screen darkens and the game becomes unresponsive. I am unable to click anywhere on the screen. I've been scratching my head about this issue for several weeks now and can't get to the bottom of it. I'd really appreciate some help/advice on the matter if anyone knows that's wrong. I've attached the error log as it was too long to add here. Many thanks, Ermes Error log:
Posted
by
Post not yet marked as solved
0 Replies
233 Views
hi! I'm beginner ios developer. I have a question about auto layout problem on Display Zoomed ( setting - Display & Brightness - Display Zoom - Zoomed) standard mode not occur problem. but user set Display Zoomed UILabel_1 covers UILabel_2 I think that UILabel_2 has constrant(Bottom space to : Button 30) finally, I Want this image. if user set Display Zoomed, if Display Zoomed mode, UILabel_2 can constrant? thank you
Posted
by
Post not yet marked as solved
1 Replies
630 Views
Hello I've been trying to build our existing iOS 14 app using Xcode 13 beta 5, and though it was working perfectly before, when trying to display some of the views on our main page this exception is consistently raised when trying to apply a snapshot on a UITableViewDiffableDataSource: Must translate autoresizing mask into constraints. looking online it seems that in previous Xcode betas have had this issue in relation to table view cells. Has anyone else been seeing this when trying to build with iOS 15? Thanks
Posted
by
Post not yet marked as solved
0 Replies
330 Views
I'm experiencing an intermittent bug with Auto Layout in the popover for Safari App Extensions. It appears that any resizing in the view controller sometimes happens before the window itself is resized, and this has a very odd appearance. I'd like to figure out how to stop this from happening. I've tried moving any constraint changes into the updateConstraints method as suggested in AppKit resources I've found, however the bug remains. For reference, here are the collapsed and expanded views: Here are the intermediate frames that I'd like to prevent: This is the function that toggles the constraints to show/hide the long text: @objc private func toggleContent(_ sender: NSButton) { if longText.isDescendant(of: contentView) { contentButton.title = "Show text" // Remove text longText.removeFromSuperview() initialBottomConstraint.isActive = true } else { // Add text contentButton.title = "Hide text" initialBottomConstraint.isActive = false longText.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(longText) NSLayoutConstraint.activate([ longText.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16.0), longText.topAnchor.constraint(equalTo: contentButton.bottomAnchor, constant: 16.0), longText.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16.0), longText.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16.0) ]) } } Edit: for screenshot size
Posted
by
Post marked as solved
1 Replies
270 Views
I've constrained a UILabel to leading, and trailing anchors, the intrinsicContentSize exceeds the anchors I've specified. Just to be sure, I added a widthAnchor as well, still no improvement. Then I added these properties: self.preferredMaxLayoutWidth = 356.25 // didn't hard code self.invalidateIntrinsicContentSize() Still the intrinsicContentSize is equal to 356.5, which is something I don't expect from my UILabel, is there anyway to fix this issue?
Posted
by
Post not yet marked as solved
1 Replies
374 Views
I'm using boundingRect to calculate the height of a String, I'm using Ubuntu as my font, and when I supply .usesFontLeading as one of the options alongside .usesLineFragmentOrigin the height is completely different to what I expect. Here is the code I'm using for reference: func height(withConstrainedWidth width: CGFloat, font: UIFont, maxLines: CGFloat = 0) -> CGFloat {         let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)         let boundingBox = self.boundingRect(with: constraintRect, options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: [             NSAttributedString.Key.font: font         ], context: nil)         var size = boundingBox.size         if maxLines > 0 {             size.height = min(size.height, CGFloat(maxLines) * font.lineHeight)         }         return ceil(size.height)     } Firstly, what does .usesFontLeading even do, and why is it that the result I'm getting an unexpected result. Note: self is String because I've extended the String class.
Posted
by
Post not yet marked as solved
4 Replies
1.7k Views
Hello! I have updated my project to Xcode 13 and iOS 15. Now the app is crashing with an error related to autoresizing masks in UITableViewCells. I have tried to change UITableViewCells Layer property in the inspector to Inferred and followed this post, but none of them are working. Have you encountered this problem. How it could be fixed? Thanks 🙏
Posted
by