SwiftUI Label text and image vertically misaligned - swift

I'm using SwiftUI's brand new Label View, running Xcode 12 beta on Big Sur.
As image I use SF Symbol and found an image named "play". But I've noticed the same problem with custom images without any bordering pixels (i.e. spacing is not caused by the image), e.g. PDF icons, so it is probably not related to the image.
In demos by Apple the Text and the image should just automatically align properly, but I do not see that.
struct ContentView: View {
var body: some View {
Label("Play", systemImage: "play")
}
}
Results in this:
Any ideas why the image (icon) and the text is vertically misaligned?
If we give the Button a background color we see more precisely the misalignment:
Label("Play", systemImage: "play")
.background(Color.red)
Results in this:

Probably a bug, so worth submitting feedback to Apple. Meanwhile here is working solution based on custom label style.
Tested with Xcode 12b
struct CenteredLabelStyle: LabelStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.icon
configuration.title
}
}
}
struct TestLabelMisalignment: View {
var body: some View {
Label("Play", systemImage: "play")
.labelStyle(CenteredLabelStyle())
}
}

#Sajjon You can add a custom View as a workaround and use Image with Text inside a HStack

Related

SwiftUI ScrollView does not respond to keyboard

Thanks for taking your time to help others :)
Problem description:
I want to bring up the ScrollView content as keyboard shows up. And I can't.
Despite, if ScrollView is turned upside down... it works!!! But I can't do that because I have to implement .contextMenu(...) and this produces an even worse bug (detailed in this post).
App must support iOS 14.
Simple code demo to show what happens.
import SwiftUI
struct ContentView: View {
#State var text: String = ""
var body: some View {
VStack {
ScrollView() {
LazyVStack {
ForEach(1..<201, id: \.self) { num in
Text("Message \(num)")
}
// .upsideDown() // With these modifiers, will prompt up the scrollView content
}
}
// .upsideDown() // With these modifiers, will prompt up the scrollView content
TextField("Your text here", text: $text)
.textFieldStyle(.roundedBorder)
.padding()
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Example app")
}
}
extension View {
func upsideDown() -> some View {
self.rotationEffect(.degrees(180))
}
}
GIF resources to see behaviour:
Good result (when upside down):
Bad result (on normal scrollView):
What we have checked?
Already tried this solution, but does not help.
Tried setting an offset to ScrollView of keyboards height when it does come up but... nothing happens.
Questions
Why does this happen when it is upside down? (and not at normal scrollview)
Why on both cases it brings up the TextField but does NOT bring up the content of Scrollview if is not upside down???

Applying padding to SwiftUI Picker with menu style is causing multiline text

I have a Picker with menu style presenting a label with an icon and text. When applying a padding to any views containing the picker, the picker will behave as though there is not enough space and run to multiple lines even though there is enough space.
Sample Code
struct ContentView: View {
let options = [
"Some Long Text"
]
private var selectedOption: Binding<String> =
.constant("Some Long Text")
var body: some View {
HStack {
Text("Some Text")
Picker("Work Type", selection: selectedOption) {
ForEach(self.options, id: \.self) {
Label($0, systemImage: "pencil.tip.crop.circle")
}
}.pickerStyle(.menu)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
VStack {
ContentView()
ContentView()
.padding()
}
}
}
This might be a layout bug in SwiftUI. There is clearly enough space to put it all in one line, even with the padding. You may want to file a bug report to Apple.
However, you can easily fix this by applying the fixedSize() modifier to your Picker:
Picker("Work Type", selection: selectedOption) {
// ...
}
.pickerStyle(.menu)
.fixedSize(horizontal: true, vertical: false)
This will make sure that the Picker doesn't concern itself with external sizing requirements, ignores the proposed with and just uses renders with its ideal size.
Please note that this might lead to unintended behavior. For example, when the user chooses a bigger font size in accessibility settings, the text in the Picker will never break and might be rendered off-screen. So make sure to think through all your use cases.
PS: You can also use a Menu instead of a Picker with .menu style. Of course, you won't get the selection behavior for free and have to do it manually, but it doesn't have this layout bug.
This seems like intended behavior to me. Because of the padding applied around the HStack (specifically at the horizontal edges) there is not enough space to render the form label on one line. If you were to change the size of the screen (for example using an iPad simulator or using landscape mode) the label would have enough room and be displayed on one line. I tested this code on an iPad simulator and it worked as expected.
If you want to force the label text to be rendered on one line, use fixedSize(horizontal:vertical:). Though this can lead to unexpected behavior.
Picker("Work Type", selection: selectedOption) {
ForEach(self.options, id: \.self) {
Label($0, systemImage: "pencil.tip.crop.circle")
}
}
.pickerStyle(.menu)
.fixedSize(horizontal: true, vertical: false)

SwiftUI List on macOS: highlighting content in the selected row?

In a SwiftUI List, when a row is selected, a blue selection is drawn in the selection, and foreground Text with the default primary color is automatically made white. For other views with custom colors, I'd like to be able to make sure they become tinted white to match the system apps. For example, see the blue dot on the selected row:
Example code:
List(selection: $selection) {
ForEach(0..<4) { index in
HStack {
Image(systemName: "circle.fill")
.foregroundColor(.blue)
Text("Test")
}
.tag(index)
}
}
Any tips on how to achieve the correct result here? 🙏
With NSTableCellView, there is the backgroundStyle property, but I couldn't find anything like this.
I've searched all of the available environment variables, and couldn't find anything appropriate.
I have also tried manually including an isSelected binding on a view for each row, but the selection binding is not updated by List until mouse-up, while the highlight is updated on mouse-down and drag, so this results in a flickery appearance and is not right either.
I'm also looking to customize not just a single SF Symbol image, but also a RoundedRectangle that is drawn with a custom foreground color that I want to become white upon selection.
Approach
Use a Label for the cell
Code
struct ContentView: View {
#State private var selection: Int?
var body: some View {
List(selection: $selection) {
ForEach(0..<4) { index in
Label("Test", systemImage: "circle.fill")
.tag(index)
}
}
}
}
Screenshot

Why does SwiftUI View background extend into safe area?

Here's a view that navigates to a 2nd view:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink {
SecondView()
} label: {
Text("Go to 2nd view")
}
}
}
}
struct SecondView: View {
var body: some View {
ZStack {
Color.red
VStack {
Text("This is a test")
.background(.green)
//Spacer() // <--If you add this, it pushes the Text to the top, but its background no longer respects the safe area--why is this?
}
}
}
}
On the 2nd screen, the green background of the Text view only extends to its border, which makes sense because the Text is a pull-in view:
Now, if you add Spacer() after the Text(), the Text view pushes to the top of the VStack, which makes sense, but why does its green background suddenly push into the safe area?
In the Apple documentation here, it says:
By default, SwiftUI sizes and positions views to avoid system defined
safe areas to ensure that system content or the edges of the device
won’t obstruct your views. If your design calls for the background to
extend to the screen edges, use the ignoresSafeArea(_:edges:) modifier
to override the default.
However, in this case, I'm not using ignoresSafeArea, so why is it acting as if I did, rather than perform the default like Apple says, which is to avoid the safe areas?
You think that you use old background modifier version.
But actually, the above code uses a new one, introduced in iOS 15 version of background modifier which by default ignores all safe area edges:
func background<S>(_ style: S, ignoresSafeAreaEdges edges: Edge.Set = .all) -> some View where S : ShapeStyle
To fix the issue just replace .background(.green) with .background(Color.green) if your app deployment target < iOS 15.0, otherwise use a new modifier: .background { Color.green }.

Swift Card Ui Design

I am just wondering what are a component to make a Ui design card for ios apps? cause I saw a lot of these cool card design on pinterest and I was wondering how to make it. Like what are the component to make such a design.
I surf the web and found that mostly they using the new SwiftUI to make that kinda design, but i was wondering is there any possible way to make it with just a regular Storyboard? and i still don't get it the component like is it using button that custom design with Xib or it using regular tableview or what?
If there's any good Library from cocoapods I would like to know either.
this is the type of card design that I want to ask :
[
also there's a link of the kinda same design as well that I saw from pinterest :
https://pin.it/694Q8hb
If anyone knows how to make this with a standard Storyboard, like what the components are and maybe if there are any libraries from cocoapods please let me know okay, cause i wanna make this type of design for my thesis, which I'm already half way of development and I just want to make a good design, and I think this card design type looks perfect on my apps. Cheers Guys :)
This can be done pretty easily in SwiftUI. You can use a VStack (or LazyVStack or LazyVGrid) to set up the list view (much easier than TableViews) and then make a custom, reusable view that looks like one of the views and set it as the Label in a Button. Here's some code (simplified) to get you started:
import SwiftUI
struct CustomView: View {
var body: some View {
VStack {
Button(action: {
print("First button pressed")
}, label: {
CustomRowView(buttonTitle: "First Button")
})
Button(action: {
print("Second button pressed")
}, label: {
CustomRowView(buttonTitle: "Second Button")
})
Button(action: {
print("Third button pressed")
}, label: {
CustomRowView(buttonTitle: "Third Button")
})
Spacer()
}
.padding()
}
}
struct CustomRowView: View {
#State var buttonTitle: String
var body: some View {
Text(buttonTitle)
.frame(maxWidth: .infinity)
.foregroundColor(.white)
.padding(.vertical, 40)
.background(Color.blue)
.cornerRadius(12)
}
}
struct CustomViewsz_Previews: PreviewProvider {
static var previews: some View {
CustomView()
}
}
The easiest way to get the look from those images is a UIView with:
A custom background
Set the corner radius with view.layer.cornerRadius
A shadow effect
UILabels and UIImageViews as children views
Probably a custom touchesBegan and touchesEnded implementation to make it behave like a button.
The views can be contained in a tableview, removing the custom dividers and stuff.
Also:
Code reuse is your friend! Put these specifications in a reusable class that you can reuse whenever you need that design.
I am Giving you the view hierarchy I hope you understand it.
Addressing First Screen from first Pic
View
TableView
Section 1 : User Profile
Section 2 : Bucket
TableViewCell.xib
Header
CollectionView
CollectionViewCell1.xib
Section 3 : Shots
TableViewCell.xib
Header
CollectionView
CollectionViewCell2.xib
Addressing the Second Screen from first pic
View
CollectionView for Options (change tableview datasource on didSelectItem)
TableView
TableViewCell.xib
Addressing Screen from Second pic
View
TableView
Header
TableViewCell
Label
CollectionView
CollectionViewCell