26

I would like to make the circle view by using react-native.

Here what I did:

circle: {
    position: 'absolute',
    borderWidth: 10,
    borderColor: '#fff',
    top: 20,
    left: 30,
    width: 150,
    height: 150,
    borderRadius: 150 / 2,
    backgroundColor: '#ED1D27',
  }

And view

<View style={styles.circle}></View>

The result is:

enter image description here

There is and outline rounded the circle.

I don't want that outline. I checked by remove the border radius and it has no outline like below:

enter image description here

I have no idea for this issue, please help me...

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Luc
  • 2,800
  • 2
  • 25
  • 46
  • Maybe someone will find a workaround but in my opinion this looks like a bug in the Stylesheet implementation as this behaviour is clearly not intended. If it's not on the bug tracker already, it should be put there. – Viktor Sec May 03 '17 at 13:25
  • Yes, I will search and put there. I also think that is a bug. – Luc May 04 '17 at 02:00
  • For what it's worth, here is a [runnable example on repl.it](https://repl.it/Hws9/0); I get no ugly border outline on my phone using "Expo client" app to view the result (Android 7). – Hugues M. May 14 '17 at 02:35
  • What version of React Native are you on? Tried your code on RN42 and there's no outline. – Ivan Wu May 03 '17 at 16:51
  • You meant RN 0.42? I am using the latest version of react-native. – Luc May 04 '17 at 02:01
  • @HuguesMoreau it happened in iOS. – Luc May 15 '17 at 02:08
  • Yes, my own test is not worth much, but the link lowers the barrier for an iOS user to test your issue :) – Hugues M. May 15 '17 at 07:20

2 Answers2

11

So I am not entirely sure why it gives that very small red border with your current rule. It could be an actual bug. Regardless if I understand correctly, you want a circle without that small red border. You can achieve that by removing your border property:

position: 'absolute',
top: 20,
left: 30,
width: 150,
height: 150,
borderRadius: 150 / 2,
backgroundColor: '#ED1D27',

Resulting in:

enter image description here

If you do want a border, a potential workaround could be:

inner: {
  position: 'relative',
  width: 150,
  height: 150,
  borderRadius: 150 / 2,
  backgroundColor: '#ED1D27',
},
outter:{
  position: 'absolute',
  paddingTop:10,
  paddingLeft:10,
  top: 20,
  left: 30,
  width: 170,
  height: 170,
  borderRadius: 160 / 2,
  backgroundColor: '#000',
},

With a view heirarchy like:

  <View style={styles.container}>
    <View style={styles.outter}>
      <View style={styles.inner}></View>
    </View>
  </View>

Resulting in:

enter image description here

klvs
  • 1,074
  • 6
  • 21
  • Maybe, it's a bug. Anyway, I am working on the solution same as you mentioned. But I don't really like this one. Maybe, I have to report this bug on the react-native repo. Thank you for your help – Luc May 08 '17 at 08:36
  • @Luc You're welcome, glad I could help. If you think this is the answer feel free to accept it. I really don't like it either. I'm actually investigating as to why it does this. Apparently it only happens on iOS too so I may be a tricky one to squash. Follow up: Apparently someone else has [run into this too](http://techqa.info/programming/question/41663739/react-native---ios---circle-with-border---circle-background-color-spilling-out-of-circle). I suppose this is the workaround for now. – klvs May 09 '17 at 00:03
3

RE-EDIT: Turns out that this border-radius issue is not isolated to working with react alone, but a general css known issue, that has been raised [and marked as fixed] numerous times. I found this link, that cites they found a solution, but also states the cause. The link in question's problem cites it initially as being related to box-shadow but from a quick google search there seems to many issues with border-radius.

Given cause:

It turns out that if your border radius is larger than the height of the element (taking in to account padding, font-size, and so forth) this visual error will occur.

A fiddle is given in the github link http://jsfiddle.net/2HqTZ/ (with html2canvas)


Earlier proposed solution answer 1- link to expo

Edited earlier proposed solution answer 2 - expo link

Current/ best solution (of mine) IMHO link

I think this is the best solution. I noted that if the border color was left out in the circedge css but left in only the circle css, the border 'outline' effect is greatly reduced. I also replaced borderRadius with borderTopLeftRadius etc after reading the known issues on caniuse.com

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.square} />
        <View style={styles.circedge}/>
        <View style={styles.circle}>
         </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 2,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#fff',
  },
  square: {
    position: 'absolute',
    top: 30,
    left: 30,
    width: 200,
    height: 100,
    backgroundColor: 'red'
  },
  circle: {
    position: 'absolute',
    borderColor: '#fff',
    top: 60,
    left: 60,
    width: 150,
    height: 150,
    borderTopLeftRadius:150/2,
    borderTopRightRadius:150/2,
    borderBottomLeftRadius:150/2,
    borderBottomRightRadius:150/2,
    backgroundColor: '#ED1D27',
  },
  circedge:{
     position: 'absolute',
     paddingTop:10,
     paddingLeft:10,
     top: 50,
     left: 50,
     width: 170,
     height: 170,
     borderTopLeftRadius:170/2,
     borderTopRightRadius:170/2,
     borderBottomLeftRadius:170/2,
    borderBottomRightRadius:170/2,
     backgroundColor: '#fff',
  }
});
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • https://repl.it/HxEh/4 I note that if i omit the border color on the edge css but leave it in only the circle css, the border 'line' effect is greatly reduced. Also replaced borderRadius with borderTopLeftRadius etc after reading the notes on caniuse – Rachel Gallen May 14 '17 at 14:32
  • 1
    Setting the borderColor to white was the solution for our issue: Strange artifacts appearing on e.g. react native Image assets with rounded corners – David Schumann Aug 12 '20 at 15:35