Problem: UIView size is "locked" when designing with Auto Layout in IB
I've uploaded a 20 second video that illustrates my problem, but let me describe it. You may recognize the project: it's right out of Apple's WWDC 2012 "Introduction to Auto Layout." I've got a simple iPhone view in Interface Builder where I've already got a button and label along the bottom. The next step is to add a UIView. Here's where things get ugly.
When I drag the UIView out of the palette in Interface Builder and onto the iPhone window, as soon as I let it go it seems to become locked in to its starting size. I place it in the upper left corner, and I'd like to drag it horizontally to the right side of the window, and vertically to where it will be up against (minus the padding) the button and label along the bottom. But, when I try to resize it, I cannot. It's stuck on its original size!
The video illustrating my problem is here:
http://www.youtube.com/watch?v=jsW4UwnCEkw
You can see me grab the right edge of the view and try to pull it along to the right side of the window, but it won't expand. You can then see me move the entire view to the right side (it retains its size) and then try the opposite: grabbing the left side and trying to expand it so that it sizes up against the left side of the window. In both cases, the view is simply locked to its original size.
What gives! Auto Layout in Interface Builder is just the most frustrating thing. How do I resize a view by dragging? Thanks!
(Note: Xcode 4.6.3.)
In IB, in the series of buttons in the lower right hand corner, click on the rightmost button:
When you do, you'll see a "When resizing Views Apply Constraints To..." pop-up menu:
Make sure that "Siblings and Ancestors" is not checked.
Related
To keep things simple, let's say I have a window containing a single view, which has auto layout constraints binding all 4 sides to the window container view with offset 0. And assume that this view also has a constraint setting its aspect ratio to a constant value. If I resize the window manually, then then window nicely maintains the desired aspect ratio. But if I click the little green zoom widget, then the window fills up the whole screen, regardless of the aspect ratio, with part of the view being above the top of the screen. Is there some way I can make zooming resize the window as big as it can be, without violating auto layout constraints?
I couldn't very well detect this problem in the delegate method windowWillResize:toSize:, because that doesn't tell me which screen it's thinking about putting the window on. I could try to fix the window size in the windowDidResize: delegate method, at which time I do know what screen it's on, but I'm not sure exactly how to do that without reinventing Auto Layout's wheel.
Apparently someone thinks I wasn't explicit enough, so I'll try again. Steps to reproduce:
In Xcode, create a new macOS App project using XIB interface.
Open MainMenu.xib and select the window.
Reshape the window to be approximately square.
Using the Attribute Inspector, set the Full Screen behavior for the window to Auxiliary Window.
Drag an Image View from the library and drop it in the window.
In the Attributes Inspector, set the image view to show the NSComputer image and scale axes independently.
Expand the image view to fill up the window content area.
With the image view selected, click the button to add new layout constraints.
Add 5 constraints, binding the 4 sides to the container, and setting the aspect ratio. (see screen shot)
Build and Run.
Observe that if you resize the window by dragging an edge or corner, the aspect ratio remains fixed.
Click the green zoom widget in the title bar of the window, and observe that the window expands without regard for the aspect ratio constraint, cutting off part of the image.
I just set up a test project exactly as you specified, and when I invoke the window zoom widget, the window expands and retains its aspect ratio i.e. it works as expected. The only thing I can think of that might be causing your issue: maybe your content hugging and content compression resistance priorities are at odds with your constraints? Mind you, I just left them at the default values and it worked fine. Unfortunately Mac/AppKit development (esp. when using IB) is rife with these kind of odd bugs and weird behaviour, probably because Apple has not given it any love in years, so bugs creep in/fester and they are clearly so DONE with developing UI the 'old fashioned way'. (Using SwiftUI to make a Mac app is just as frustrating, in different ways, so I'll stick with what I know). FYI, I used Xcode 13.4.1 to create this test project. Good luck!
I am new to Xcode. In the Designer mode the screen looks like this:
And the properties are:
When executed the result is scrunched to the upper left corner - both in Simulator and on the actual device:
I could not find any additional LayoutManager type of settings. What is supposed to be done here?
So there is a tiny magic icon in the bottom right called Add New Alignment Constraints:
Here is where it is at in the bigger scheme of life:
Now it becomes clear what to do: click on both of the checkboxes:
Horizontally in Container
Vertically in Container
I wish these things would be included in the Menu-accessible actions instead of needing to hunt every tiny and incomprehensible icon in the gui.
I'm trying to update my app for the new iPhone X. After reading about the safe area feature and the check box "Safe Area Relative Margins" in each UIObject's "Size Inspector" (ruler tab), I didn't think this would be too bad. However, that feature does not seem to be working for me.
Nothing changed for regular iPhones, which is good, however for the X, the top of my app overlaps the top inset of the phone by a third. Is there any known way to fix this* or something I'm missing?
*By fix this, I mean make it so that my objects start below the outcrop, like the second picture.
What is happening:
Desired Behavior (from https://arstechnica.com/gadgets/2017/11/how-devs-updated-their-apps-for-the-iphone-xs-screen-and-the-notch/):
I too have wasted hours on this. And while I cannot answer your question of why this is broken in Xcode, I can provide a solution.
But first let me note than in Xcode 11.3.1, I experience the same issue in a new project created from scratch.
Set up your storyboard:
On your storyboard, select "Use Safe Area Layout Guides". This will add a safe area to each of your ViewControllers. It will also require you to target iOS9+. You might be able to skip this step though (see step 4 below).
View your storyboard as iPhone 4s.
Fix each ViewController:
Select all views under the top-level view.
Click Editor | Embed In | View Without Inset. This creates a new view and puts all your views inside.
Make this new view expand to the safe area by adding safe area constraints (by control-dragging your new view onto the top level view).
Leading space to safe area
Top space to safe area
Trailing space to safe area
Bottom space to safe area
If you did not opt into using a safe area storyboard above, you may be able to create four equivalent constraints by using the Top Layout Guide, Bottom Layout Guide, and the left/right sides of the top-level view. This may not work in landscape though. And I did not test this.
Set your new view as transparent.
Give your new view a name like "SafeAreaView".
In iOS 11, margins are inset from the safe area. Thus, your zero top margin becomes a 20 top margin — explaining your screen shot. If that's not what you want, set the view's insetsLayoutMarginsFromSafeArea property to false.
In my case modalPresentationStyle = .fullScreen has to be set to the view controller being presented
I'm trying to implement a counter view that shows items count in NSTableCellView.
The weird thing is it gets moved to right automatically after resizing window or scrolling the NSOutlineView which has the NSTableCellView.
Here is some screenshots of it. You will be able to see the counter view which shows "42" being moved to right after resize the window.
Following is the init state of the counter view.
Following is the state after resize window.
Following is difference between init position and the position after resize window.
Cell Autoresize Property
Count Button Autoresize Property
I'm guessing this is related to scrollview but I was not able to find more clues about this issue. BTW, official Mail App on Mac works as same with this but the difference is the counter of the Mail app is being changed right after launch so it does not being moved after resize window.
Anybody knows about this? Any thoughts would be appreciated.
Here is a link that allows you to download the sample project that apple provides and you can reproduce what I'm facing.
Sidebar Demo App that providing by Apple.
You just need to change Deployment target before you run it from Project -> Targets -> Deployment Info section.
If I interpret the autoresizing screenshots correctly, the '42' control is behaving as specified. You've pinned the right side of the '42' counter to right side of the cell. And that is where it stays, glued to the right side of the cell.
The cell is pinned to the left side of the tableview and that is where it stays, glued to the left side of the tableview. You have not pinned the right side of the cell to anything and so when you resize, the right side goes where it pleases, only maintaining the specified width of the cell. If you want to glue the right side of the cell to the width of the tableView or column (not sure which it is), pin it. Do specify a minimum width for the cell. If you don't specify a maximum width, or fixed width, the cell will stretch and shrink with resizing. If you fix the width, only the right view or column will stretch.
I have perhaps a beginner's question but have not found any solutions addressing this specific problem after searching endlessly on stackoverflow and other forums.
My detail view in the storyboard appears to be too zoomed in. At this point, zooming in and out simply zooms in and out on the storyboard, but not the detail view specifically.
Indicators of this are that the alignment arrow to the left of my button is not positioned at half-latitude of the Detail View box, although when I align my button to this marker I do see a crosshair indicating that my button is "centered".
Detail View - Further Out
Upon simulation, it is apparent that using these crosshairs produces a run-time alignment far right and up from being centered. Where my button is currently placed seems to be in the center of the simulator screen but would like my guidelines to allow the button to be centered using the guidelines suggested.
Simulator View
A) How do I "zoom out" within the Detail View so I can see the entire screen in the detail view?
B) How can I center my object if the guidelines are inherently off-center?
Thank you so much for you advice and input.
Are you using constraints to keep the various controls in place etc...?
If not try:
Select all elements in the UI of the detail view. Click on an empty spot somewhere in the View and press CMD + A.
Click on resolve auto layout issues at the bottom right of the Nib editor. (it's the right most icon)
Click on Clear Constraints
Click on resolve auto layout issues again, then Reset to Suggested Constraints
From here, tweak your constraints as per the UI you're working with i.e. iPhone, iPad and whatever orientation it's in etc...
I forgot to mention how I fixed the "too zoomed-in" problem.
In the storyboard controller, on the left navigator panel, within the file inspector, I simply unchecked "Use Size Classes" within the Interface Builder Document section. My view controller changed into a normal iPhone shape and everything finally fit into place.