How do you create a NSTouchBarItem that uses the remaining width in the Touch Bar? - macos-sierra

I'm trying to create a NSButton that takes up the remaining space on the Touch Bar. My goal is to have a button similar to the "Search or enter website name" button in Safari.
I've looked at setting constraints with widthAnchor, but I'm completely lost at how to set it properly.

NSButton has an intrinsicContentSize that fits its content, and a default horizontal contentHuggingPriority of 750 (NSLayoutPriorityDefaultHigh), meaning it will strongly try to match that content width.
You can decrease that hugging priority (e.g. [button setContentHuggingPriority:1.0 forOrientation:NSLayoutConstraintOrientationHorizontal]) in order to make it more able to stretch away from that content, and in this case fill up the extra space in the touch bar.

Related

Unity: Button spacing differs at different screen resolutions

In my game (Unity 2017.3.1f1) I'm using a ScrollView that's filled with buttons at runtime.
At 1080p:
And at 1440p:
The blue dots show the constraints of the Content, the whole thing (including the grey part below) makes up the Scroll View. No matter how big/small I make the Content, the border doesn't vanish at 1440p but is instead resized with the Content.
The width of the ScrollView is set through a surrounding panel
The buttons all use the same pefab (Inspector: click) - 1st image below
The buttons are arranged by a Vertical Layout Group on the Content (Inspector: click) - 2nd image below
As you can see in the last screenshot, the Vertical Layout Group is set to force expand the button's width and control its size, which isn't set anywhere else. I also don't set any padding/spacing in the Vertical Layout Group and yet there's an empty space between the two buttons.
I want it to look the same at 1080p and 1440p. What am I missing?

Xcode: align objects with the View Controller margins

I'm starting to study Swift with the help of this Apple's guide : https://itunes.apple.com/it/book/app-development-with-swift/id1219117996?mt=11
Now i am stuck on a small problem that i would like to understand how to solve, but from the guide it is not very clear.
I'm at the 398 page : “Guided Project: Personality Quiz”.
Initially i have to place 4 labels in all the corners of the view controller.
From the guide:
“To hold your emoji in their respective corners on all screen sizes,
you'll need to add two constraints to each label. Begin by selecting
the top-left label and clicking the Add New Constraints button. Enable
the top and leading constraints and set them both to 0 pixels,
ensuring there's no space between the edges of the label and the
margins of the view. By default, the top of a view has a 20-pixel
margin, and the left and right sides have 16 pixels of margin. So when
you enter 0 pixels, you're actually telling the label to position
itself 20 pixels from the top and 16 pixels from the left edge of the
view. Add these two constraints”
So, following the guide, i place the first label in the upper left corner, and add the two constraints
And this is the result. The label is aligned to the Safe Area and not to the view margins.
Looking to another project (not mine), i think the two constraints that i need are these (or something similar):
I'd describe that as a bug in Xcode. You asked for margin-relative constraints and you didn't get them. That's not very nice!
You'll just have to edit the constraints afterward. In your final screen shot, double click a constraint. Use the pop-up menu to change the Safe Area constraint to a Superview constraint. Then choose from the menu again to check Relative to Margin. Then you might have to fix the Constant value. Do that for the other constraint too.
Either that or just don't use the constraint popover to begin with. Instead, draw your constraint by control-dragging from the button and use the HUD that appears. Hold Option in the HUD to get superview margin constraints.
EDIT Ooooh, here's another workaround; before you form your constraints, hide the safe area layout guides (uncheck the 4th checkbox in this screen shot):
Now the constraint popover works correctly.

Swift - Auto constraints

I am trying to use the auto size classes from the storyboard on xcode 7 to position UI elements in a controller. The problem that I'm running into is that when I try to use the "Add missing constraints" function (located at the bottom right corner of the console), it positions my UI elements correctly except for the last elements (pictures describe better). The first image below shows the storyboard file where I just want 3 buttons (stacked above eachother) to be the same width and length to be on the top right corner of any screen.
However, when I add constraints and run the simulations, it seems like the top two buttons are positioned correctly with the correct length and width but the third button is out of place (image below).
So my question is, am I forgetting a step to make all buttons position themselves? Or should I try to convert everything to a percentage and place UI elements based on the percentage of the screen (if so, how would I go about doing that)?
I've also tried adding another blank button (removing the button label) underneath the 3rd button and adding constraints like that but it didn't work for me. Let me know if you have any suggestions, thanks!
In you case, Autolayout the constraints you need to give to UIButton is 4 constrains.
Leading
Trailing
Width
Height
If you miss any of them, then surely you will get an error. So, what's your error is?
To the third UIButton, you have not given the height, while to the above two buttons you have given.
So, just remove the bottom constraint of UIButton and give the equal height to above UIButton.
FYI, never use Add Missing Constraints without any confirmation from your side.
Update:
Check this video to remove trailing or leading margin:
http://sendvid.com/1h8deg18
You can actually see the solution in action if you use the Preview screen while setting up your auto layout constraints. I just created a similar view and buttons and stepped through the process. I coloured the buttons and named them to make things obvious.
I added the three buttons. At this point, none of the buttons show up in the preview.
I then setup the auto layout constraints for Button1. If you want the buttons anchored to the top right, then you don't need to worry about the leading constraint. You need width, height, top, and trailing.
Now Button1 will snap to it's position in the top right corner of the preview screen.
Now do the same thing for Button2. Set width, height, top (vertical space to Button1), and trailing.
Button2 will now snap into place in the preview.
Now the same thing again for Button3. You anchored the first button to the top right of the screen. Then Button2 to the bottom of Button1 and the right edge of the screen. Then again for Button3. You could also align the edges of the 2nd and 3rd buttons, if you prefer that to trailing space.
Now you'll see in the preview that your buttons are correctly positioned, regardless of device.
As long as you specify height and width for each button, you don't need to worry about the left edge or the bottom edge of the screen at all. They each know to "stick" to the top right and they know what size to be.
** Note: If you're not familiar with the "Preview" option...
With your storyboard open, hold Option and select storyboard again to get another copy of the storyboard on the right side. Highlight the view controller you are interested in on the left side. On the right side, select the Preview option as shown below.
Now you have your storyboard and the preview side by side, so you can see the exact impact of any auto layout changes you make. You can also add or remove devices to the preview.

ios autolayout constraints to resize view for screen size

I am new to ios development and still trying to get hang of autolayout. The problem that I am facing can be better explained with images below
Now I am removing the "Login with a social network" on iphone 4. What I want is how to center the google plus icon between the "OR Label" and "Start without registering" button
and it should also resize to fit between them.
what kind of constraints does it need to achieve that. If someone can point me in the right direction that would be great help. I have been breaking my head over this entire day. Thanks.
to center [g+] between [or] and [start ..] buttons:
you need to do two things.. on the storyboard:
select [g+] and [or]
select editor->pin vertical spacing
and then
select [g+] and [start]
select editor->pin vertical spacing
you should see a vertical line show up between the two objects each time. This is to indicate that that space will always be the same weather on a 3.5" or a 4" iPhone display.
I don't see why you want the g+ button to resize.. that actually goes agains the UI guidelines which instruct that different users shouldn't have different user experiences (see the reason behind why there isn't a replacement for the iOS 7.0 deprecated sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: method)
The same logic that discourages changing font sizes on labels/buttons depending on form factor etc resulting in different users having different experiences, is the same logic that would discourage you from scaling up or down your [g+] button depending on form factor circumstances as well.
If you're just trying to get the "g+" graphic centered between the "OR" label and the "Start without registering" label, you can:
Add a view (that you won't really see because you'll make it transparent, but it's useful for alignment purposes) whose top constraint is linked to the "OR" label and whose bottom constraint is linked to the "Start without registering" label:
Then put the "g+" graphic inside that view, aligning its center vertical and horizontal constraints with its superview's.
If you do that, the "g+" button will stay in the middle between those two labels (assuming, of course, that the "Start without registering" label has a bottom constraint to its superview, i.e. It's pinned to the bottom of the view).
In terms of resizing that button, I'd personally advise against that, if you wanted, you could, for example, remove that transparent view we created above, and just do that trick with the "g+" image/button itself. You will want to set the content size of that imageview to be "Aspect Fit" (so it doesn't get distorted).
Again, I really don't like that latter approach, but you can do it.
I gather that you want to remove the "Log in with social network" label at runtime. There are two approaches for that:
A simple approach is to create an IBOutlet for the height constraint for that label, e.g., logInWithSocialNetworkHeightConstraint and then you can do:
self.logInWithSocialNetwork.text = nil;
self.logInWithSocialNetworkHeightConstraint.constant = nil;
[self.view layoutIfNeeded];
The alternative is to define a constraint between the "OR" label and the "g+" graphic's appropriate view. This, itself, has two possible approaches:
You could define this in IB, but define it with a priority less than 1000. Because the constraints for the "Login with ..." label will have higher priority, when that label is there, those constraints will take precedence. If you you remove the label:
[self.logInWithSocialNetwork removeFromSuperview];
[self.view layoutIfNeeded];
Then the new constraint you defined will then come into play.
You could alternatively remove the "Login with ..." label and then programmatically add a new constraint between the "OR" button and the "g+" graphic's view.

Can we increase button height if the button label text is so long in iPhone?

I have put 4 buttons with 4 labels on the button as button title using interface builder. My button label shows the showroom name, email id...etc with font size 17 correct. But if Showroom name or email id get bigger then my button label shows information with small font size which looks odd and not according to apple HIG.
So can we increase button height programmatically to show button label title with consistent font size 17.
you can use 'sizeToFit' function to RESIZE button to it's content optimum display,
or
you can use 'sizeThatFits:CGSizeZero' to actually GET the optimum frame and do whatever you want with it.
OBS: this will only work if 'sizeThatFits' is bigger that original size, so i suggest you to set size to something like (1,1) before calling the functions above
Did you try with, something like this:
button.frame = CGRectMake(0,0,80,200)
to change the height of the button?