首页 文章

在iOS上使NativeScript ListView透明化

提问于
浏览
7

我试图让NativeScript <ListView> 在iOS上透明,但我失败了 . 我在https://groups.google.com/forum/#!topic/nativescript/-MIWcQo-l6k找到了关于该主题的旧帖子,但是当我尝试解决方案时,它对我不起作用 . 这是我的完整代码:

/* app.css */
Page { background-color: black; }

<!-- main-page.xml -->
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="loaded">
  <ListView id="list-view" items="{{ items }}" itemLoading="itemLoading">
    <ListView.itemTemplate>
      <Label text="{{ name }}" />
    </ListView.itemTemplate>
  </ListView>
</Page>

// main-page.js
var ios = require("utils/utils");
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;

var page;
var items = new ObservableArray([]);
var pageData = new Observable();

exports.loaded = function(args) {
  page = args.object;
  page.bindingContext = pageData;

  // Toss a few numbers in the list for testing
  items.push({ name: "1" });
  items.push({ name: "2" });
  items.push({ name: "3" });

  pageData.set("items", items);
};

exports.itemLoading = function(args) {
  var cell = args.ios;
  if (cell) {
    // Use ios.getter for iOS 9/10 API compatibility
    cell.backgroundColor = ios.getter(UIColor.clearColor);
  }
}

任何帮助,将不胜感激 . 谢谢!

3 回答

  • 10

    不要忘记将listview设置为透明,似乎本身就有backgroundcolor

    ListView{
            background-color: transparent;
        }
    
  • 2

    目前使用NativeScript 2.4可以实现以下功能

    var cell = args.ios;
    if (cell) {
      cell.selectionStyle = UITableViewCellSelectionStyleNone
    }
    

    如果你想改变选择高亮颜色这里是一个简单的方法,我没有测试性能,但它在iPhone 6上运行正常 .

    import { Color } from 'color';
    cell.selectedBackgroundView = UIView.alloc().initWithFrame(CGRectMake(0, 0, 0, 0));
    let blue = new Color('#3489db');
    cell.selectedBackgroundView.backgroundColor = blue.ios
    
  • 1

    不确定是否有更好的方法可以做到这一点,但这对我来说在iOS上使用NativeScript 2.4对我来说都是有用的A)使ListView背景透明,B)在点击项目时改变颜色:

    let lvItemLoading = (args) => {
       let cell = args.ios;
       if (cell) {
          // Make the iOS listview background transparent
          cell.backgroundColor = ios.getter(cell, UIColor.clearColor);
    
          // Create new background view for selected state
          let bgSelectedView = UIView.alloc().init();
          bgSelectedView.backgroundColor = new Color("#777777").ios;
          bgSelectedView.layer.masksToBounds = true;
          cell.selectedBackgroundView = bgSelectedView;
       }
    };
    

相关问题