Moving Horizontal ScrollView up top SwiftUI - swiftui

I am trying to move the images in Horizontal ScrollView up top. They are currently at the bottom. I have tried adding NavigationView, .padding(), Spacer(), Adding a ZStack as a parent to HStack, and HStack(alignment: .top). I have been stuck for 3 days.
Is it possible to have Horizontal ScrollView up top?
Here is the code:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView(.horizontal) {
HStack(spacing: 15) {
Group {
Image("OutdoorDining1")
.frame(width: 230, height: 148)
Image("EatVegan")
.frame(width: 230, height: 148)
Image("CityView")
.frame(width: 230, height: 148)
Image("LiveMusic")
.frame(width: 230, height: 148)
}
Group {
Image("Seafood")
.frame(width: 230, height: 148)
Image("Blackowned")
.frame(width: 230, height: 148)
Image("Hookah")
.frame(width: 230, height: 148)
Image("OpenTilLate")
.frame(width: 230, height: 148)
}
Group {
Image("CentralAmerica")
.frame(width: 230, height: 148)
Image("Asian")
.frame(width: 230, height: 148)
Image("Mediterranean")
.frame(width: 230, height: 148)
}
Group {
Image("Steakhouses")
.frame(width: 230, height: 148)
Image("Kosher")
.frame(width: 230, height: 148)
}

Hi Christina and welcome to SO! You can use ChrisR's suggestion VStack + Spacer() - it's works, but if you building more complex UI and want to add ScrollView(.vertical) and more views use next hierarchy
struct ContentView: View {
var body: some View {
ZStack(alignment: .top) {
NavigationView {
ScrollView(.vertical) {
VStack {
ScrollView(.horizontal) {
HStack(spacing: 15) {
Group {
Image("EatVegan")
.frame(width: 230, height: 148)
Image("CityView")
.frame(width: 230, height: 148)
}
Group {
Image("Seafood")
.frame(width: 230, height: 148)
Image("Blackowned")
.frame(width: 230, height: 148)
}
}
}
// add other views here
}
}
}
}
}
}

Related

SWIFTUI : Wide space appear on top as switch between tabs

Basically, I am working on a project in which there is a navigation process, therefore, I have to implement navigationview with tabview. As app loads, initial screen appear(homeview) normal, but an issue arises as soon I switch between tab option, wide space appears. So can anyone guide me in it? I am adding code with this query.
Even I have tried to add navigationView on individual tabitem and on it wide space issue gets resolve but new issue appear that tabbar doesnot hides on navigationlink even i have implemented "uitabbar.appearance.ishidden = true" on respective view
'''
import SwiftUI
struct TabBarView: View {
#State private var selection = 0
var body: some View {
NavigationView{
TabView(selection: $selection) {
HomeView()
.tabItem {
Image("tab_icon1")
.resizable()
.frame(width: 10, height: 10, alignment: .center)
}
.tag(0)
// Text("Search Tab")
SettingView()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image("tab_icon2")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
}
.tag(1)
//tab_new
// Text("New Tab")
ActivityListView()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image("tab_new")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
}
.tag(2)
HomeView()
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image("tab_icon3")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
}
.tag(3)
Text("Profile Tab")
.font(.system(size: 30, weight: .bold, design: .rounded))
.tabItem {
Image("tab_icon4")
.resizable()
.frame(width: 50, height: 50, alignment: .center)
}
.tag(4)
}
.edgesIgnoringSafeArea(.top)
.accentColor(Color("Pink"))
.onAppear() {
UITabBar.appearance().barTintColor = UIColor(named: "DarkGray")
UITabBar.appearance().backgroundColor = UIColor(named: "DarkGray")
}
//
}
}
}
'''

Show part of next item in TabView

I want to show part of next item in tabview as following design
I managed to show one item per page but I couldn't show part of next one. Here's my code
GeometryReader { proxy in
TabView(selection: $viewModel.selectedTabIndex) {
ForEach(self.viewModel.data?.perspective ?? []) { item in
HStack(spacing: 20) {
Image("icon")
.resizable()
.scaledToFit()
.frame(width: 24, height: 24, alignment: .center)
Text(item.name)
.font(.system(size: 16, weight: .medium))
.foregroundColor(Color.white)
.tag(item.id)
Spacer()
}
.frame(width: proxy.size.width - 70, height: 40, alignment: .center)
.padding(.horizontal)
.background(.red)
.cornerRadius(5)
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
}
.frame(height: 40, alignment: .center)

VStack .leading alignment

I can't get rid of this "leading" padding inside one of my VStacks.
I set alignment to "leading" for its parent VStack but it still shows some spacing on the left (for the text and red rounded rectangle). They were supposed to be placed on the left next to the blue edge on screenshot.
Any ideas on why this padding appears there?
Here's the code:
import SwiftUI
struct RegisterView: View {
var body: some View {
VStack(spacing: 0) {
HStack(alignment: .top) {
Text("Register")
.font(.custom("NotoSans-Regular", size: 24))
.fontWeight(.bold)
.foregroundColor(Color.tertiaryTitleColor)
}
.frame(width: 299, height: 39)
.padding(.top, 78)
HStack(spacing: 13) {
BroviderButton(imageName: "googleLogo")
BroviderButton(imageName: "facebookLogo")
}
.padding(.top, 47)
VStack(alignment: .leading, spacing: 0) {
Text("Register with E-mail")
.font(.custom("NotoSans-Regular", size: 16))
RoundedRectangle(cornerRadius: 25)
.fill(Color.red)
.frame(width: 40, height: 20)
}
.frame(width: 279)
.padding(.top, 61)
Spacer()
}
.frame(maxHeight: /*#START_MENU_TOKEN#*/.infinity/*#END_MENU_TOKEN#*/)
.frame(maxWidth: /*#START_MENU_TOKEN#*/.infinity/*#END_MENU_TOKEN#*/)
.background(Color.loginBackgroundColor)
.ignoresSafeArea()
}
}
struct RegisterView_Previews: PreviewProvider {
static var previews: some View {
RegisterView()
}
}
struct BroviderButton: View {
var imageName: String
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 27.5)
.frame(width: 133, height: 56, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.shadow(color: Color.dropShadowColor, radius: 1, x: 10, y: 20)
.offset(y: 15)
.opacity(0.2)
.blur(radius: 20)
.opacity(0.2)
VStack {
VStack {
Image(imageName)
.resizable()
.scaledToFit()
}
.frame(height: 28, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
}
.frame(width: 133, height: 56, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.foregroundColor(/*#START_MENU_TOKEN#*/.blue/*#END_MENU_TOKEN#*/)
.background(Color.loginBackgroundColor)
.cornerRadius(27.5)
.opacity(1)
}
}
}
You need also apply alignment to frame of container, like
VStack(alignment: .leading, spacing: 0) {
Text("Register with E-mail")
.font(.custom("NotoSans-Regular", size: 16))
RoundedRectangle(cornerRadius: 25)
.fill(Color.red)
.frame(width: 40, height: 20)
}
.frame(width: 279) // << do you really need this? why?
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
.padding(.top, 61)

Implementing Box-Shadow in SwiftUI

I'm trying to implement the following css code in SwiftUI
background: #FFFFFF;
box-shadow: 0px 10px 20px rgba(223, 227, 240, 0.2);
border-radius: 27.5px;
It should look like this (barely visible shadow below the button):
Here's the code i'm currently experimenting with:
import SwiftUI
struct RegisterView: View {
var body: some View {
VStack(spacing: 0) {
HStack(alignment: .top) {
Text("Register")
.font(.custom("NotoSans-Regular", size: 24))
.fontWeight(.bold)
.foregroundColor(Color.tertiaryTitleColor)
}
.frame(width: 299, height: 39)
.padding(.top, 78)
HStack {
BroviderButton(imageName: "googleLogo")
BroviderButton(imageName: "facebookLogo")
}
.padding(.top, 47)
Spacer()
}
.frame(maxHeight: .infinity)
.frame(maxWidth: .infinity)
.background(Color.loginBackgroundColor)
.ignoresSafeArea()
}
}
struct RegisterView_Previews: PreviewProvider {
static var previews: some View {
RegisterView()
}
}
struct BroviderButton: View {
var imageName: String
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 27.5)
.stroke(Color.dropShadowColor,lineWidth: 1)
.frame(width: 133, height: 56, alignment: .center)
.shadow(color: .black, radius: 2, x: 1, y: 2)
VStack {
VStack {
Image(imageName)
.resizable()
.scaledToFit()
}
.frame(height: 28, alignment: .center)
}
.frame(width: 133, height: 56, alignment: .center)
.foregroundColor(.blue)
.background(Color.loginButtonBackgroundColor)
.cornerRadius(27.5)
.opacity(1)
}
}
}
And here's how it looks like atm:
I don't understand how to translate x:10 & y:20 to SwiftUI code and also can't find the way to blur the shadow (20%). Any ideas are welcome.
Also can't figure out why that RoundedRectangle stays visible after i put VStack on top of it.. I was thinking it will hide everything but the shadow below the button.
As far as I see there are two issues here to be fixed (although of course I don't have your colors and images):
ZStack {
RoundedRectangle(cornerRadius: 27.5)
.fill(Color.dropShadowColor) // << here fill, not stroke !!
.frame(width: 133, height: 56, alignment: .center)
.shadow(color: .black, radius: 2, x: 0, y: 2) // << no offset by x
VStack {
VStack {
Image(imageName)
.resizable()
.scaledToFit()
}
.frame(height: 28, alignment: .center)
}
.frame(width: 133, height: 56, alignment: .center)
.foregroundColor(.blue)
.background(Color.loginButtonBackgroundColor) // << should be opaque color !!
.cornerRadius(27.5)
.opacity(1)
}

relativeSize isn't adjusting HStack dimensions

When .relativeSize is called on HStack{}, the borders do not come closer. Indicative of dimensions not being adjusted.
I've already tried rearranging the sequence of method calls on HStack, such that .relativeSize is called before and after .frame and .border
struct ContentView : View {
var body: some View{
VStack(alignment: .center, spacing: 1){
//Top box holding a rectangle
HStack{
Text("Test")
}
.frame(width: 355, height: 99, alignment: .center)
.relativeSize(width: 0.95, height: 0.95)
.border(Color.blue, width: 2)
//Bottom box holding 3 recangles
HStack{
Text("Test")
}
.frame(width: 355, height: 47, alignment: .center)
.border(Color.red, width: 1)
}
.frame(width: 355, height: 148, alignment: .center)
.border(Color.black, width: 1)
//body paren
}
}
Simply add a Spacer() before and after the text, and remove the .frame() modifier. This way the HStack will expand laterally as much as 0.95 the size of the containing VStack. Is this what you want?
struct ContentView : View {
var body: some View{
VStack(alignment: .center, spacing: 1){
//Top box holding a rectangle
HStack{
Spacer()
Text("Test")
Spacer()
}
.relativeSize(width: 0.95, height: 0.95)
.border(Color.blue, width: 2)
//Bottom box holding 3 recangles
HStack{
Text("Test")
}
.frame(width: 355, height: 47, alignment: .center)
.border(Color.red, width: 1)
}
.frame(width: 355, height: 148, alignment: .center)
.border(Color.black, width: 1)
//body paren
}
}