You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guide/create-custom-native-elements.md
+252-55
Original file line number
Diff line number
Diff line change
@@ -5,17 +5,20 @@ contributors:
5
5
- NathanWalker
6
6
---
7
7
8
-
When working on view markup with NativeScript, a collection of elements you interact with are registered for you like [GridLayout](https://docs.nativescript.org/ui/grid-layout), [Button](https://docs.nativescript.org/ui/button), [Label](https://docs.nativescript.org/ui/label), etc. These are just commonly used elements.
8
+
When working with view markup in NativeScript, you interact with a set of pre-registered elements, such as [`GridLayout`](https://docs.nativescript.org/ui/grid-layout), [`Button`](https://docs.nativescript.org/ui/button), and [`Label`](https://docs.nativescript.org/ui/label). These are commonly referred to as **"view components."**
9
9
10
-
You can create new custom native elements or extend existing ones to enhance behavior for all sorts of cases.
10
+
You can also create custom native elements or extend existing ones to add specialized functionality to your application.
11
11
12
-
Let's first look at how you would register new elements across all flavors and then we'll discuss how to build one, starting with a simple example of a custom View class.
12
+
This guide demonstrates how to:
13
+
14
+
- Register custom view elements across all NativeScript flavors.
15
+
- Implement a simple custom view component, starting from a basic class definition:
13
16
14
17
```ts
15
18
import { View } from'@nativescript/core'
16
19
17
20
exportclassCheckboxextendsView {
18
-
//impl
21
+
//implementation details
19
22
}
20
23
```
21
24
@@ -138,109 +141,303 @@ You can now use it like anything else:
138
141
</Tab>
139
142
</Tabs>
140
143
141
-
## Creating New Views
144
+
## Creating Custom View Components
142
145
143
-
Creating a new view element for use across anything in NativeScript (Angular, React, Solid, Svelte, TypeScript by itself, Vue, etc) is exactly the same.
146
+
The process for creating new NativeScript view components is consistent across Angular, React, Solid, Svelte, TypeScript, and Vue.
144
147
145
-
### Custom View Anatomy
148
+
### Anatomy of a Custom View
146
149
147
-
There's only 2 **fundamental** required aspects to any custom NativeScript view component (with 2 _optional_ additions):
150
+
Every custom NativeScript view must have:
148
151
149
-
1. Extend any NativeScript View.
150
-
2. (**required**) `createNativeView`: Construct and return any platformnative view.
- (**required**) A class extending any NativeScript View.
153
+
- (**required**) `createNativeView`: Construct and return a platform-native view._Note: It's only required if you want to override what's being returned from the extended class._
154
+
- (_optional_) `initNativeView`: Perform initialization after creation.
155
+
- (_optional_) `disposeNativeView`: Cleanup resources when removed.
153
156
154
-
Let's look at those within the context of an example:
157
+
Example:
155
158
156
159
```ts
157
-
// 1. (required) Extend any NativeScript View
160
+
import { View } from'@nativescript/core'
161
+
158
162
exportclassCustomViewextendsView {
159
-
// 2. (required) Construct and return any platform native view
160
163
createNativeView() {
161
-
// return instance of UIView or android.view.View;
164
+
// return UIView or android.view.View instance
162
165
}
163
166
164
-
// 3. (optional) initialize anything
165
-
initNativeView() {}
167
+
initNativeView() {
168
+
// initialization code
169
+
}
166
170
167
-
// 4. (optional) cleanup anything
168
-
disposeNativeView() {}
171
+
disposeNativeView() {
172
+
// cleanup code
173
+
}
169
174
}
170
175
```
171
176
172
-
Let's explain each point respectively.
177
+
#### Extending Existing Views
173
178
174
-
1. (required) extend any NativeScript View: **any** NativeScript view, for example these are all valid:
179
+
You can extend **any**existing NativeScript view, for example:
4.`disposeNativeView`: Destroy and cleanup anything if needed.
211
-
212
-
You will find many examples of this pattern throughout @nativescript/core, for example:
205
+
#### Initialization and Cleanup
213
206
214
-
-[Button implementation for iOS](https://github.com/NativeScript/NativeScript/blob/96af6fa83e586a1c443c8b179701450d803e12aa/packages/core/ui/button/index.ios.ts#L21)
215
-
-[Button implementation for Android](https://github.com/NativeScript/NativeScript/blob/96af6fa83e586a1c443c8b179701450d803e12aa/packages/core/ui/button/index.android.ts#L72)
207
+
-`initNativeView`: Perform setup after the view has been created.
208
+
-`disposeNativeView`: Free resources as needed.
216
209
217
-
All NativeScript plugins follow this exact same pattern as well, for example:
210
+
Explore more examples within NativeScript core and plugins:
218
211
219
-
-[@nativescript/flutter for iOS](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.ios.ts#L40)
220
-
-[@nativescript/flutter for Android](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.android.ts#L80)
-[Custom Flutter view example (iOS)](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.ios.ts#L40)
215
+
-[Custom Flutter view example (Android)](https://github.com/NativeScript/ui-kit/blob/dca883ada479a6d0982abbcb01a51f661d927812/packages/flutter/index.android.ts#L80)
221
216
222
-
You can also learn a lot about this with additional details such as using Cocoapods, Gradle and more in [this blog post series](https://blog.nativescript.org/create-a-custom-view-plugin-marquee-label/).
217
+
For additional details on advanced topics like using Cocoapods or Gradle, refer to [this blog series](https://blog.nativescript.org/create-a-custom-view-plugin-marquee-label/).
223
218
224
-
### Implementing within a Project
219
+
##Project Structure for Custom Views
225
220
226
-
Implementing custom native elements at any moment within a project starts with creating a folder for your new element followed by the implementation. A common folder organization is as follows:
221
+
Create a dedicated folder for your component, such as:
227
222
228
223
```bash
229
224
./checkbox
230
225
common.ts
231
226
index.android.ts
232
-
index.d.ts
233
227
index.ios.ts
228
+
index.d.ts
234
229
```
235
230
236
-
This represents a new encapsulated custom native `<Checkbox />` element sharing common code via `common.ts` with each platform implementation represented via it's suffix. The `index.d.ts` is a convenience allowing one to simply import via the folder to register the element anywhere, for example:
231
+
This structure encapsulates platform-specific implementations and shared logic, simplifying imports and registration:
237
232
238
233
```ts
239
234
import { Checkbox } from'./checkbox'
235
+
```
236
+
237
+
## Real-World Example: Custom Checkbox
238
+
239
+
Let's create a `<Checkbox>` component that behaves consistently on iOS and Android like this:
240
+
241
+
<iframestyle="width: 100%; min-height: 200px; aspect-ratio: 16 / 9;"src="https://www.youtube.com/embed/08uip43irOM"title="Creating custom elements with NativeScript"frameborder="0"allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"allowfullscreen></iframe>
242
+
243
+
See the full working example on StackBlitz: https://stackblitz.com/edit/nativescript-create-custom-elements-checkbox?file=src%2Fapp%2Fcheckbox%2Fcommon.ts
240
244
241
-
// register custom element using examples above for the flavor you're using
245
+
In NativeScript, creating custom UI components can be straightforward and powerful. In this guide, you'll learn how to build a simple, reusable Checkbox component using only built-in modules from `@nativescript/core`. We'll leverage a combination of `GridLayout`, `Label`, and Material Design icons.
246
+
247
+
### Step-by-Step Guide
248
+
249
+
#### 1. Extend `GridLayout`
250
+
251
+
The base structure of our Checkbox will utilize `GridLayout`, which serves as a container for the checkbox icon:
252
+
253
+
```typescript
254
+
import { GridLayout } from'@nativescript/core'
255
+
256
+
exportclassCheckboxCommonextendsGridLayout {}
257
+
```
258
+
259
+
#### 2. Initialize Checkbox with a Font Icon
260
+
261
+
Next, we'll add a Label to serve as our checkbox icon, using Material Design icons for the visual representation.
262
+
263
+
```typescript
264
+
import { GridLayout, Label, Color } from'@nativescript/core'
Creating custom components in NativeScript allows you to:
434
+
435
+
- Build complex components from simple elements
436
+
- Utilize powerful styling and animations
437
+
- Easily manage and expose customizable properties
438
+
439
+
Whether creating fully native views or hybrid custom components, NativeScript provides flexibility and efficiency to meet your application's needs.
440
+
244
441
## Customize Existing Elements?
245
442
246
-
Rather than create a new element from scratch, you may want to customize existing view elements. Learn about how to [extend any element for custom beavhior here](/guide/customizing-view-elements).
443
+
Rather than create new elements from scratch, you may want to just adjust existing view elements. Learn about how to [extend any element for custom behavior here](/guide/customizing-view-elements).
Copy file name to clipboardExpand all lines: content/guide/customizing-view-elements.md
+57-81
Original file line number
Diff line number
Diff line change
@@ -5,35 +5,37 @@ contributors:
5
5
- NathanWalker
6
6
---
7
7
8
-
You can customize existing elements by extending them to enable new behavior.
8
+
You can extend existing NativeScript UI elements to customize their behavior or appearance.
9
9
10
-
For example, what if the [ListPicker](/ui/list-picker)should have a bigger font size with a different highlight color?
10
+
This guide demonstrates how to customize the font size and highlight color of the [ListPicker](/ui/list-picker)element.
11
11
12
-
## Customize Existing View Element
12
+
## Customizing an Existing View Element
13
13
14
-
To customize the ListPicker, the steps are similar to creating new elements [outlined here](/guide/create-custom-native-elements). Start by creating a folder for the custom element followed by the customizations needed:
14
+
To customize an existing element such as `ListPicker`, follow a similar process as described in the [Creating Custom Native Elements](/guide/create-custom-native-elements) guide.
15
+
16
+
### Directory Structure
17
+
18
+
Begin by creating a new folder structure for your customized ListPicker:
15
19
16
20
```bash
17
21
./list-picker-custom
18
-
common.ts
19
-
index.android.ts
20
-
index.d.ts
21
-
index.ios.ts
22
+
├── common.ts
23
+
├── index.android.ts
24
+
├── index.d.ts
25
+
└── index.ios.ts
22
26
```
23
27
24
-
In this case we want to allow the picker to display a bigger font size and possibly a different highlight color.
28
+
### Adjusting Font Size on iOS
25
29
26
-
Supporting a bigger font size on iOS with ListPicker starts by researching how [UIPickerView](https://developer.apple.com/documentation/uikit/uipickerview?language=objc) can be modified to display a bigger font size. As we can see from core itself, the ListPicker is simply just a [UIPickerView alone](https://github.com/NativeScript/NativeScript/blob/96af6fa83e586a1c443c8b179701450d803e12aa/packages/core/ui/list-picker/index.ios.ts#L15).
30
+
To modify the font size for a ListPicker on iOS, first refer to Apple's [UIPickerView documentation](https://developer.apple.com/documentation/uikit/uipickerview?language=objc). Since NativeScript's ListPicker directly utilizes `UIPickerView`, any native iOS modifications apply.
27
31
28
-
This means that all the information available on iOS development applies directly to NativeScript as well.
32
+
For instance, this [Stack Overflow solution](https://stackoverflow.com/a/48744047) describes how to change the font size.
29
33
30
-
For instance, even this [Stack Overflow answer](https://stackoverflow.com/a/48744047) regarding the exact same question applies.
34
+
### Extending ListPicker for Custom Font Size
31
35
32
-
### Extend Existing ListPicker
36
+
Create the following implementation in `index.ios.ts`, extending the default `ListPicker`:
33
37
34
-
In our `index.ios.ts` we can extend the existing ListPicker to add our own [iOS delegate](https://developer.apple.com/documentation/uikit/uipickerviewdelegate?language=objc) which implements the method suggested to support making the font size bigger. We can even start with an exact replica of the [ListPicker](https://github.com/NativeScript/NativeScript/blob/main/packages/core/ui/list-picker/index.ios.ts) in core and distill it down to just what we need.
This custom element now implements the `UIPickerViewDelegate`'s [pickerViewViewForRowForComponentReusingView](<https://developer.apple.com/documentation/uikit/uipickerviewdelegate/pickerview(_:viewforrow:forcomponent:reusing:)?language=objc>) allowing us to customize the font size with this line:
131
+
In this code, the font size is customized in the delegate's `pickerViewViewForRowForComponentReusingView` method:
125
132
126
-
```ts
133
+
```typescript
127
134
label.font=UIFont.systemFontOfSize(26)
128
135
```
129
136
130
-
#### Customize Highlight Color
131
-
132
-
Similar to the above solution, customizing a UIPickerView highlight color has been discussed before. As suggested in [this discussion](https://stackoverflow.com/a/56392417) we can modify the `backgroundColor` of the appropriate subviews.
133
-
134
-
Anytime we want to modify iOS subviews, we can do so within NativeScript's `onLoaded` method.
135
-
136
-
```ts
137
-
exportclassCustomListPickerextendsListPicker {
138
-
// ...
139
-
140
-
onLoaded() {
141
-
super.onLoaded()
142
-
// Optional: customize selection highlight bar
143
-
for (let i =0; i<this.nativeViewProtected.subviews.count; i++) {
You can also customize the highlight color by modifying the subviews' background colors in the `onLoaded()` lifecycle hook (shown in the example above). Adjust RGB and alpha values to achieve your desired color.
163
140
164
-
You can register `CustomListPicker` for use anywhere in your view markup using [this guide](/guide/create-custom-native-elements).
141
+
### Registering Your Custom Element
165
142
166
-
### Functional StackBlitz Example
143
+
Register your new `CustomListPicker` element following the steps outlined in [Creating Custom Native Elements](/guide/create-custom-native-elements).
167
144
168
-
You can try this example yourself on StackBlitz here:
Try changing the font size or highlight color live on StackBlitz to see the changes to behavior yourself.
147
+
Experiment live with this custom ListPicker in [this StackBlitz demo](https://stackblitz.com/edit/nativescript-customize-listpicker?file=src%2Fapp%2Fitem%2Flist-picker-custom%2Findex.ios.ts). Modify the font size or highlight color directly and observe the immediate changes.
0 commit comments